1<?php 2 3/** 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. 7 * 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: 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 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. 19 * 20 * PHP Version 5 21 * 22 * @file CAS/PGTStorage/AbstractStorage.php 23 * @category Authentication 24 * @package PhpCAS 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 28 */ 29 30/** 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(). 33 * 34 * @class CAS_PGTStorage_File 35 * @category Authentication 36 * @package PhpCAS 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 40 * 41 * 42 * @ingroup internalPGTStorageFile 43 */ 44 45class CAS_PGTStorage_File extends CAS_PGTStorage_AbstractStorage 46{ 47 /** 48 * @addtogroup internalPGTStorageFile 49 * @{ 50 */ 51 52 /** 53 * a string telling where PGT's should be stored on the filesystem. Written by 54 * PGTStorageFile::PGTStorageFile(), read by getPath(). 55 * 56 * @private 57 */ 58 public $_path; 59 60 /** 61 * This method returns the name of the directory where PGT's should be stored 62 * on the filesystem. 63 * 64 * @return the name of a directory (with leading and trailing '/') 65 * 66 * @private 67 */ 68 public function getPath() 69 { 70 return $this->_path; 71 } 72 73 // ######################################################################## 74 // DEBUGGING 75 // ######################################################################## 76 77 /** 78 * This method returns an informational string giving the type of storage 79 * used by the object (used for debugging purposes). 80 * 81 * @return an informational string. 82 * @public 83 */ 84 public function getStorageType() 85 { 86 return "file"; 87 } 88 89 /** 90 * This method returns an informational string giving informations on the 91 * parameters of the storage.(used for debugging purposes). 92 * 93 * @return an informational string. 94 * @public 95 */ 96 public function getStorageInfo() 97 { 98 return 'path=`' . $this->getPath() . '\''; 99 } 100 101 // ######################################################################## 102 // CONSTRUCTOR 103 // ######################################################################## 104 105 /** 106 * The class constructor, called by CAS_Client::SetPGTStorageFile(). 107 * 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 110 * 111 * @return void 112 * 113 * @public 114 */ 115 public function __construct($cas_parent, $path) 116 { 117 phpCAS::traceBegin(); 118 // call the ancestor's constructor 119 parent::__construct($cas_parent); 120 121 if (empty($path)) { 122 $path = CAS_PGT_STORAGE_FILE_DEFAULT_PATH; 123 } 124 // check that the path is an absolute path 125 if (getenv("OS") == "Windows_NT") { 126 if (!preg_match('`^[a-zA-Z]:`', $path)) { 127 phpCAS::error('an absolute path is needed for PGT storage to file'); 128 } 129 } else { 130 if ($path[0] != '/') { 131 phpCAS::error('an absolute path is needed for PGT storage to file'); 132 } 133 134 // store the path (with a leading and trailing '/') 135 $path = preg_replace('|[/]*$|', '/', $path); 136 $path = preg_replace('|^[/]*|', '/', $path); 137 } 138 139 $this->_path = $path; 140 phpCAS::traceEnd(); 141 } 142 143 // ######################################################################## 144 // INITIALIZATION 145 // ######################################################################## 146 147 /** 148 * This method is used to initialize the storage. Halts on error. 149 * 150 * @return void 151 * @public 152 */ 153 public function init() 154 { 155 phpCAS::traceBegin(); 156 // if the storage has already been initialized, return immediatly 157 if ($this->isInitialized()) { 158 return; 159 } 160 // call the ancestor's method (mark as initialized) 161 parent::init(); 162 phpCAS::traceEnd(); 163 } 164 165 // ######################################################################## 166 // PGT I/O 167 // ######################################################################## 168 169 /** 170 * This method returns the filename corresponding to a PGT Iou. 171 * 172 * @param string $pgt_iou the PGT iou. 173 * 174 * @return a filename 175 * @private 176 */ 177 public function getPGTIouFilename($pgt_iou) 178 { 179 phpCAS::traceBegin(); 180 $filename = $this->getPath() . "phpcas-" . hash("sha256", $pgt_iou); 181// $filename = $this->getPath().$pgt_iou.'.plain'; 182 phpCAS::trace("Sha256 filename:" . $filename); 183 phpCAS::traceEnd(); 184 return $filename; 185 } 186 187 /** 188 * This method stores a PGT and its corresponding PGT Iou into a file. Echoes a 189 * warning on error. 190 * 191 * @param string $pgt the PGT 192 * @param string $pgt_iou the PGT iou 193 * 194 * @return void 195 * 196 * @public 197 */ 198 public function write($pgt, $pgt_iou) 199 { 200 phpCAS::traceBegin(); 201 $fname = $this->getPGTIouFilename($pgt_iou); 202 if (!file_exists($fname)) { 203 touch($fname); 204 // Chmod will fail on windows 205 @chmod($fname, 0600); 206 if ($f = fopen($fname, "w")) { 207 if (fputs($f, $pgt) === false) { 208 phpCAS::error('could not write PGT to `' . $fname . '\''); 209 } 210 phpCAS::trace('Successful write of PGT to `' . $fname . '\''); 211 fclose($f); 212 } else { 213 phpCAS::error('could not open `' . $fname . '\''); 214 } 215 } else { 216 phpCAS::error('File exists: `' . $fname . '\''); 217 } 218 phpCAS::traceEnd(); 219 } 220 221 /** 222 * This method reads a PGT corresponding to a PGT Iou and deletes the 223 * corresponding file. 224 * 225 * @param string $pgt_iou the PGT iou 226 * 227 * @return the corresponding PGT, or FALSE on error 228 * 229 * @public 230 */ 231 public function read($pgt_iou) 232 { 233 phpCAS::traceBegin(); 234 $pgt = false; 235 $fname = $this->getPGTIouFilename($pgt_iou); 236 if (file_exists($fname)) { 237 if (!($f = fopen($fname, "r"))) { 238 phpCAS::error('could not open `' . $fname . '\''); 239 } else { 240 if (($pgt = fgets($f)) === false) { 241 phpCAS::error('could not read PGT from `' . $fname . '\''); 242 } 243 phpCAS::trace('Successful read of PGT to `' . $fname . '\''); 244 fclose($f); 245 } 246 // delete the PGT file 247 @unlink($fname); 248 } else { 249 phpCAS::error('No such file `' . $fname . '\''); 250 } 251 phpCAS::traceEnd($pgt); 252 return $pgt; 253 } 254 255 /** @} */ 256} 257