4 * @file CAS/PGTStorage/pgt-file.php
5 * Basic class for PGT file storage
9 * @class PGTStorageFile
10 * The PGTStorageFile class is a class for PGT file storage. An instance of
11 * this class is returned by CASClient::SetPGTStorageFile().
13 * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr>
15 * @ingroup internalPGTStorageFile
18 class PGTStorageFile extends PGTStorage
21 * @addtogroup internalPGTStorageFile
26 * a string telling where PGT's should be stored on the filesystem. Written by
27 * PGTStorageFile::PGTStorageFile(), read by getPath().
34 * This method returns the name of the directory where PGT's should be stored
37 * @return the name of a directory (with leading and trailing '/')
47 * a string telling the format to use to store PGT's (plain or xml). Written by
48 * PGTStorageFile::PGTStorageFile(), read by getFormat().
55 * This method returns the format to use when storing PGT's on the filesystem.
57 * @return a string corresponding to the format used (plain or xml).
63 return $this->_format;
66 // ########################################################################
68 // ########################################################################
71 * This method returns an informational string giving the type of storage
72 * used by the object (used for debugging purposes).
74 * @return an informational string.
77 function getStorageType()
83 * This method returns an informational string giving informations on the
84 * parameters of the storage.(used for debugging purposes).
86 * @return an informational string.
89 function getStorageInfo()
91 return 'path=`'.$this->getPath().'\', format=`'.$this->getFormat().'\'';
94 // ########################################################################
96 // ########################################################################
99 * The class constructor, called by CASClient::SetPGTStorageFile().
101 * @param $cas_parent the CASClient instance that creates the object.
102 * @param $format the format used to store the PGT's (`plain' and `xml' allowed).
103 * @param $path the path where the PGT's should be stored
107 function PGTStorageFile($cas_parent,$format,$path)
109 phpCAS::traceBegin();
110 // call the ancestor's constructor
111 $this->PGTStorage($cas_parent);
113 if (empty($format) ) $format = CAS_PGT_STORAGE_FILE_DEFAULT_FORMAT;
114 if (empty($path) ) $path = CAS_PGT_STORAGE_FILE_DEFAULT_PATH;
116 // check that the path is an absolute path
117 if (getenv("OS")=="Windows_NT"){
119 if (!preg_match('`^[a-zA-Z]:`', $path)) {
120 phpCAS::error('an absolute path is needed for PGT storage to file');
127 if ( $path[0] != '/' ) {
128 phpCAS::error('an absolute path is needed for PGT storage to file');
131 // store the path (with a leading and trailing '/')
132 $path = preg_replace('|[/]*$|','/',$path);
133 $path = preg_replace('|^[/]*|','/',$path);
136 $this->_path = $path;
137 // check the format and store it
139 case CAS_PGT_STORAGE_FILE_FORMAT_PLAIN:
140 case CAS_PGT_STORAGE_FILE_FORMAT_XML:
141 $this->_format = $format;
144 phpCAS::error('unknown PGT file storage format (`'.CAS_PGT_STORAGE_FILE_FORMAT_PLAIN.'\' and `'.CAS_PGT_STORAGE_FILE_FORMAT_XML.'\' allowed)');
149 // ########################################################################
151 // ########################################################################
154 * This method is used to initialize the storage. Halts on error.
160 phpCAS::traceBegin();
161 // if the storage has already been initialized, return immediatly
162 if ( $this->isInitialized() )
164 // call the ancestor's method (mark as initialized)
169 // ########################################################################
171 // ########################################################################
174 * This method returns the filename corresponding to a PGT Iou.
176 * @param $pgt_iou the PGT iou.
181 function getPGTIouFilename($pgt_iou)
183 phpCAS::traceBegin();
184 $filename = $this->getPath().$pgt_iou.'.'.$this->getFormat();
185 phpCAS::traceEnd($filename);
190 * This method stores a PGT and its corresponding PGT Iou into a file. Echoes a
193 * @param $pgt the PGT
194 * @param $pgt_iou the PGT iou
198 function write($pgt,$pgt_iou)
200 phpCAS::traceBegin();
201 $fname = $this->getPGTIouFilename($pgt_iou);
202 if ( $f=fopen($fname,"w") ) {
203 if ( fputs($f,$pgt) === FALSE ) {
204 phpCAS::error('could not write PGT to `'.$fname.'\'');
208 phpCAS::error('could not open `'.$fname.'\'');
214 * This method reads a PGT corresponding to a PGT Iou and deletes the
215 * corresponding file.
217 * @param $pgt_iou the PGT iou
219 * @return the corresponding PGT, or FALSE on error
223 function read($pgt_iou)
225 phpCAS::traceBegin();
227 $fname = $this->getPGTIouFilename($pgt_iou);
228 if ( !($f=fopen($fname,"r")) ) {
229 phpCAS::trace('could not open `'.$fname.'\'');
231 if ( ($pgt=fgets($f)) === FALSE ) {
232 phpCAS::trace('could not read PGT from `'.$fname.'\'');
237 // delete the PGT file
240 phpCAS::traceEnd($pgt);