4 * Licensed to Jasig under one or more contributor license
5 * agreements. See the NOTICE file distributed with this work for
6 * additional information regarding copyright ownership.
8 * Jasig licenses this file to you under the Apache License,
9 * Version 2.0 (the "License"); you may not use this file except in
10 * compliance with the License. You may obtain a copy of the License at:
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 * @file CAS/PGTStorage/AbstractStorage.php
23 * @category Authentication
25 * @author Pascal Aubry <pascal.aubry@univ-rennes1.fr>
26 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
27 * @link https://wiki.jasig.org/display/CASC/phpCAS
31 * The CAS_PGTStorage_File class is a class for PGT file storage. An instance of
32 * this class is returned by CAS_Client::SetPGTStorageFile().
34 * @class CAS_PGTStorage_File
35 * @category Authentication
37 * @author Pascal Aubry <pascal.aubry@univ-rennes1.fr>
38 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
39 * @link https://wiki.jasig.org/display/CASC/phpCAS
42 * @ingroup internalPGTStorageFile
45 class CAS_PGTStorage_File extends CAS_PGTStorage_AbstractStorage
48 * @addtogroup internalPGTStorageFile
53 * a string telling where PGT's should be stored on the filesystem. Written by
54 * PGTStorageFile::PGTStorageFile(), read by getPath().
61 * This method returns the name of the directory where PGT's should be stored
64 * @return the name of a directory (with leading and trailing '/')
73 // ########################################################################
75 // ########################################################################
78 * This method returns an informational string giving the type of storage
79 * used by the object (used for debugging purposes).
81 * @return an informational string.
84 function getStorageType()
90 * This method returns an informational string giving informations on the
91 * parameters of the storage.(used for debugging purposes).
93 * @return an informational string.
96 function getStorageInfo()
98 return 'path=`'.$this->getPath().'\'';
101 // ########################################################################
103 // ########################################################################
106 * The class constructor, called by CAS_Client::SetPGTStorageFile().
108 * @param CAS_Client $cas_parent the CAS_Client instance that creates the object.
109 * @param string $path the path where the PGT's should be stored
115 function __construct($cas_parent,$path)
117 phpCAS::traceBegin();
118 // call the ancestor's constructor
119 parent::__construct($cas_parent);
122 $path = CAS_PGT_STORAGE_FILE_DEFAULT_PATH;
124 // check that the path is an absolute path
125 if (getenv("OS")=="Windows_NT") {
127 if (!preg_match('`^[a-zA-Z]:`', $path)) {
128 phpCAS::error('an absolute path is needed for PGT storage to file');
133 if ( $path[0] != '/' ) {
134 phpCAS::error('an absolute path is needed for PGT storage to file');
137 // store the path (with a leading and trailing '/')
138 $path = preg_replace('|[/]*$|', '/', $path);
139 $path = preg_replace('|^[/]*|', '/', $path);
142 $this->_path = $path;
146 // ########################################################################
148 // ########################################################################
151 * This method is used to initialize the storage. Halts on error.
158 phpCAS::traceBegin();
159 // if the storage has already been initialized, return immediatly
160 if ($this->isInitialized()) {
163 // call the ancestor's method (mark as initialized)
168 // ########################################################################
170 // ########################################################################
173 * This method returns the filename corresponding to a PGT Iou.
175 * @param string $pgt_iou the PGT iou.
180 function getPGTIouFilename($pgt_iou)
182 phpCAS::traceBegin();
183 $filename = $this->getPath().$pgt_iou.'.plain';
184 phpCAS::traceEnd($filename);
189 * This method stores a PGT and its corresponding PGT Iou into a file. Echoes a
192 * @param string $pgt the PGT
193 * @param string $pgt_iou the PGT iou
199 function write($pgt,$pgt_iou)
201 phpCAS::traceBegin();
202 $fname = $this->getPGTIouFilename($pgt_iou);
203 if (!file_exists($fname)) {
205 // Chmod will fail on windows
206 @chmod($fname, 0600);
207 if ($f=fopen($fname, "w")) {
208 if (fputs($f, $pgt) === false) {
209 phpCAS::error('could not write PGT to `'.$fname.'\'');
211 phpCAS::trace('Successful write of PGT to `'.$fname.'\'');
214 phpCAS::error('could not open `'.$fname.'\'');
217 phpCAS::error('File exists: `'.$fname.'\'');
223 * This method reads a PGT corresponding to a PGT Iou and deletes the
224 * corresponding file.
226 * @param string $pgt_iou the PGT iou
228 * @return the corresponding PGT, or FALSE on error
232 function read($pgt_iou)
234 phpCAS::traceBegin();
236 $fname = $this->getPGTIouFilename($pgt_iou);
237 if (file_exists($fname)) {
238 if (!($f=fopen($fname, "r"))) {
239 phpCAS::error('could not open `'.$fname.'\'');
241 if (($pgt=fgets($f)) === false) {
242 phpCAS::error('could not read PGT from `'.$fname.'\'');
244 phpCAS::trace('Successful read of PGT to `'.$fname.'\'');
247 // delete the PGT file
250 phpCAS::error('No such file `'.$fname.'\'');
252 phpCAS::traceEnd($pgt);