3 * Copyright © 2003-2010, The ESUP-Portail consortium & the JA-SIG Collaborative.
\r
4 * All rights reserved.
\r
6 * Redistribution and use in source and binary forms, with or without
\r
7 * modification, are permitted provided that the following conditions are met:
\r
9 * * Redistributions of source code must retain the above copyright notice,
\r
10 * this list of conditions and the following disclaimer.
\r
11 * * Redistributions in binary form must reproduce the above copyright notice,
\r
12 * this list of conditions and the following disclaimer in the documentation
\r
13 * and/or other materials provided with the distribution.
\r
14 * * Neither the name of the ESUP-Portail consortium & the JA-SIG
\r
15 * Collaborative nor the names of its contributors may be used to endorse or
\r
16 * promote products derived from this software without specific prior
\r
17 * written permission.
\r
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
\r
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
\r
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
\r
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
\r
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
\r
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
\r
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
\r
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
\r
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
31 * @file CAS/PGTStorage/pgt-file.php
\r
32 * Basic class for PGT file storage
\r
36 * @class PGTStorageFile
\r
37 * The PGTStorageFile class is a class for PGT file storage. An instance of
\r
38 * this class is returned by CASClient::SetPGTStorageFile().
\r
40 * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr>
\r
42 * @ingroup internalPGTStorageFile
\r
45 class PGTStorageFile extends PGTStorage
\r
48 * @addtogroup internalPGTStorageFile
\r
53 * a string telling where PGT's should be stored on the filesystem. Written by
\r
54 * PGTStorageFile::PGTStorageFile(), read by getPath().
\r
61 * This method returns the name of the directory where PGT's should be stored
\r
62 * on the filesystem.
\r
64 * @return the name of a directory (with leading and trailing '/')
\r
70 return $this->_path;
\r
74 * a string telling the format to use to store PGT's (plain or xml). Written by
\r
75 * PGTStorageFile::PGTStorageFile(), read by getFormat().
\r
82 * This method returns the format to use when storing PGT's on the filesystem.
\r
84 * @return a string corresponding to the format used (plain or xml).
\r
88 function getFormat()
\r
90 return $this->_format;
\r
93 // ########################################################################
\r
95 // ########################################################################
\r
98 * This method returns an informational string giving the type of storage
\r
99 * used by the object (used for debugging purposes).
\r
101 * @return an informational string.
\r
104 function getStorageType()
\r
110 * This method returns an informational string giving informations on the
\r
111 * parameters of the storage.(used for debugging purposes).
\r
113 * @return an informational string.
\r
116 function getStorageInfo()
\r
118 return 'path=`'.$this->getPath().'\', format=`'.$this->getFormat().'\'';
\r
121 // ########################################################################
\r
123 // ########################################################################
\r
126 * The class constructor, called by CASClient::SetPGTStorageFile().
\r
128 * @param $cas_parent the CASClient instance that creates the object.
\r
129 * @param $format the format used to store the PGT's (`plain' and `xml' allowed).
\r
130 * @param $path the path where the PGT's should be stored
\r
134 function PGTStorageFile($cas_parent,$format,$path)
\r
136 phpCAS::traceBegin();
\r
137 // call the ancestor's constructor
\r
138 $this->PGTStorage($cas_parent);
\r
140 if (empty($format) ) $format = CAS_PGT_STORAGE_FILE_DEFAULT_FORMAT;
\r
141 if (empty($path) ) $path = CAS_PGT_STORAGE_FILE_DEFAULT_PATH;
\r
143 // check that the path is an absolute path
\r
144 if (getenv("OS")=="Windows_NT"){
\r
146 if (!preg_match('`^[a-zA-Z]:`', $path)) {
\r
147 phpCAS::error('an absolute path is needed for PGT storage to file');
\r
154 if ( $path[0] != '/' ) {
\r
155 phpCAS::error('an absolute path is needed for PGT storage to file');
\r
158 // store the path (with a leading and trailing '/')
\r
159 $path = preg_replace('|[/]*$|','/',$path);
\r
160 $path = preg_replace('|^[/]*|','/',$path);
\r
163 $this->_path = $path;
\r
164 // check the format and store it
\r
166 case CAS_PGT_STORAGE_FILE_FORMAT_PLAIN:
\r
167 case CAS_PGT_STORAGE_FILE_FORMAT_XML:
\r
168 $this->_format = $format;
\r
171 phpCAS::error('unknown PGT file storage format (`'.CAS_PGT_STORAGE_FILE_FORMAT_PLAIN.'\' and `'.CAS_PGT_STORAGE_FILE_FORMAT_XML.'\' allowed)');
\r
173 phpCAS::traceEnd();
\r
176 // ########################################################################
\r
178 // ########################################################################
\r
181 * This method is used to initialize the storage. Halts on error.
\r
187 phpCAS::traceBegin();
\r
188 // if the storage has already been initialized, return immediatly
\r
189 if ( $this->isInitialized() )
\r
191 // call the ancestor's method (mark as initialized)
\r
193 phpCAS::traceEnd();
\r
196 // ########################################################################
\r
198 // ########################################################################
\r
201 * This method returns the filename corresponding to a PGT Iou.
\r
203 * @param $pgt_iou the PGT iou.
\r
205 * @return a filename
\r
208 function getPGTIouFilename($pgt_iou)
\r
210 phpCAS::traceBegin();
\r
211 $filename = $this->getPath().$pgt_iou.'.'.$this->getFormat();
\r
212 phpCAS::traceEnd($filename);
\r
217 * This method stores a PGT and its corresponding PGT Iou into a file. Echoes a
\r
218 * warning on error.
\r
220 * @param $pgt the PGT
\r
221 * @param $pgt_iou the PGT iou
\r
225 function write($pgt,$pgt_iou)
\r
227 phpCAS::traceBegin();
\r
228 $fname = $this->getPGTIouFilename($pgt_iou);
\r
229 if(!file_exists($fname)){
\r
230 if ( $f=fopen($fname,"w") ) {
\r
231 if ( fputs($f,$pgt) === FALSE ) {
\r
232 phpCAS::error('could not write PGT to `'.$fname.'\'');
\r
236 phpCAS::error('could not open `'.$fname.'\'');
\r
239 phpCAS::error('File exists: `'.$fname.'\'');
\r
241 phpCAS::traceEnd();
\r
245 * This method reads a PGT corresponding to a PGT Iou and deletes the
\r
246 * corresponding file.
\r
248 * @param $pgt_iou the PGT iou
\r
250 * @return the corresponding PGT, or FALSE on error
\r
254 function read($pgt_iou)
\r
256 phpCAS::traceBegin();
\r
258 $fname = $this->getPGTIouFilename($pgt_iou);
\r
259 if (file_exists($fname)){
\r
260 if ( !($f=fopen($fname,"r")) ) {
\r
261 phpCAS::trace('could not open `'.$fname.'\'');
\r
263 if ( ($pgt=fgets($f)) === FALSE ) {
\r
264 phpCAS::trace('could not read PGT from `'.$fname.'\'');
\r
269 // delete the PGT file
\r
272 phpCAS::trace('No such file `'.$fname.'\'');
\r
274 phpCAS::traceEnd($pgt);
\r