phpCAS  version 1.4.0
File.php
Go to the documentation of this file.
1 <?php
2 
46 {
58  var $_path;
59 
68  function getPath()
69  {
70  return $this->_path;
71  }
72 
73  // ########################################################################
74  // DEBUGGING
75  // ########################################################################
76 
84  function getStorageType()
85  {
86  return "file";
87  }
88 
96  function getStorageInfo()
97  {
98  return 'path=`'.$this->getPath().'\'';
99  }
100 
101  // ########################################################################
102  // CONSTRUCTOR
103  // ########################################################################
104 
115  function __construct($cas_parent,$path)
116  {
118  // call the ancestor's constructor
119  parent::__construct($cas_parent);
120 
121  if (empty($path)) {
123  }
124  // check that the path is an absolute path
125  if (getenv("OS")=="Windows_NT" || strtoupper(substr(PHP_OS,0,3)) == 'WIN') {
126 
127  if (!preg_match('`^[a-zA-Z]:`', $path)) {
128  phpCAS::error('an absolute path is needed for PGT storage to file');
129  }
130 
131  } else {
132 
133  if ( $path[0] != '/' ) {
134  phpCAS::error('an absolute path is needed for PGT storage to file');
135  }
136 
137  // store the path (with a leading and trailing '/')
138  $path = preg_replace('|[/]*$|', '/', $path);
139  $path = preg_replace('|^[/]*|', '/', $path);
140  }
141 
142  $this->_path = $path;
144  }
145 
146  // ########################################################################
147  // INITIALIZATION
148  // ########################################################################
149 
156  function init()
157  {
159  // if the storage has already been initialized, return immediatly
160  if ($this->isInitialized()) {
161  return;
162  }
163  // call the ancestor's method (mark as initialized)
164  parent::init();
166  }
167 
168  // ########################################################################
169  // PGT I/O
170  // ########################################################################
171 
180  function getPGTIouFilename($pgt_iou)
181  {
183  $filename = $this->getPath()."phpcas-".hash("sha256", $pgt_iou);
184 // $filename = $this->getPath().$pgt_iou.'.plain';
185  phpCAS::trace("Sha256 filename:" . $filename);
187  return $filename;
188  }
189 
201  function write($pgt,$pgt_iou)
202  {
204  $fname = $this->getPGTIouFilename($pgt_iou);
205  if (!file_exists($fname)) {
206  touch($fname);
207  // Chmod will fail on windows
208  @chmod($fname, 0600);
209  if ($f=fopen($fname, "w")) {
210  if (fputs($f, $pgt) === false) {
211  phpCAS::error('could not write PGT to `'.$fname.'\'');
212  }
213  phpCAS::trace('Successful write of PGT to `'.$fname.'\'');
214  fclose($f);
215  } else {
216  phpCAS::error('could not open `'.$fname.'\'');
217  }
218  } else {
219  phpCAS::error('File exists: `'.$fname.'\'');
220  }
222  }
223 
234  function read($pgt_iou)
235  {
237  $pgt = false;
238  $fname = $this->getPGTIouFilename($pgt_iou);
239  if (file_exists($fname)) {
240  if (!($f=fopen($fname, "r"))) {
241  phpCAS::error('could not open `'.$fname.'\'');
242  } else {
243  if (($pgt=fgets($f)) === false) {
244  phpCAS::error('could not read PGT from `'.$fname.'\'');
245  }
246  phpCAS::trace('Successful read of PGT to `'.$fname.'\'');
247  fclose($f);
248  }
249  // delete the PGT file
250  @unlink($fname);
251  } else {
252  phpCAS::error('No such file `'.$fname.'\'');
253  }
254  phpCAS::traceEnd($pgt);
255  return $pgt;
256  }
257 
260 }
261 ?>
__construct($cas_parent, $path)
Definition: File.php:115
static error($msg)
Definition: CAS.php:580
const CAS_PGT_STORAGE_FILE_DEFAULT_PATH
Definition: CAS.php:155
read($pgt_iou)
Definition: File.php:234
static traceEnd($res='')
Definition: CAS.php:675
static trace($str)
Definition: CAS.php:616
write($pgt, $pgt_iou)
Definition: File.php:201
static traceBegin()
Definition: CAS.php:628
getPGTIouFilename($pgt_iou)
Definition: File.php:180