pgt-file.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /*
  3. * Copyright © 2003-2010, The ESUP-Portail consortium & the JA-SIG Collaborative.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the ESUP-Portail consortium & the JA-SIG
  15. * Collaborative nor the names of its contributors may be used to endorse or
  16. * promote products derived from this software without specific prior
  17. * written permission.
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  22. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  25. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /**
  30. * @file CAS/PGTStorage/pgt-file.php
  31. * Basic class for PGT file storage
  32. */
  33. /**
  34. * @class PGTStorageFile
  35. * The PGTStorageFile class is a class for PGT file storage. An instance of
  36. * this class is returned by CASClient::SetPGTStorageFile().
  37. *
  38. * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr>
  39. *
  40. * @ingroup internalPGTStorageFile
  41. */
  42. class PGTStorageFile extends PGTStorage
  43. {
  44. /**
  45. * @addtogroup internalPGTStorageFile
  46. * @{
  47. */
  48. /**
  49. * a string telling where PGT's should be stored on the filesystem. Written by
  50. * PGTStorageFile::PGTStorageFile(), read by getPath().
  51. *
  52. * @private
  53. */
  54. var $_path;
  55. /**
  56. * This method returns the name of the directory where PGT's should be stored
  57. * on the filesystem.
  58. *
  59. * @return the name of a directory (with leading and trailing '/')
  60. *
  61. * @private
  62. */
  63. function getPath()
  64. {
  65. return $this->_path;
  66. }
  67. /**
  68. * a string telling the format to use to store PGT's (plain or xml). Written by
  69. * PGTStorageFile::PGTStorageFile(), read by getFormat().
  70. *
  71. * @private
  72. */
  73. var $_format;
  74. /**
  75. * This method returns the format to use when storing PGT's on the filesystem.
  76. *
  77. * @return a string corresponding to the format used (plain or xml).
  78. *
  79. * @private
  80. */
  81. function getFormat()
  82. {
  83. return $this->_format;
  84. }
  85. // ########################################################################
  86. // DEBUGGING
  87. // ########################################################################
  88. /**
  89. * This method returns an informational string giving the type of storage
  90. * used by the object (used for debugging purposes).
  91. *
  92. * @return an informational string.
  93. * @public
  94. */
  95. function getStorageType()
  96. {
  97. return "file";
  98. }
  99. /**
  100. * This method returns an informational string giving informations on the
  101. * parameters of the storage.(used for debugging purposes).
  102. *
  103. * @return an informational string.
  104. * @public
  105. */
  106. function getStorageInfo()
  107. {
  108. return 'path=`'.$this->getPath().'\', format=`'.$this->getFormat().'\'';
  109. }
  110. // ########################################################################
  111. // CONSTRUCTOR
  112. // ########################################################################
  113. /**
  114. * The class constructor, called by CASClient::SetPGTStorageFile().
  115. *
  116. * @param $cas_parent the CASClient instance that creates the object.
  117. * @param $format the format used to store the PGT's (`plain' and `xml' allowed).
  118. * @param $path the path where the PGT's should be stored
  119. *
  120. * @public
  121. */
  122. function PGTStorageFile($cas_parent,$format,$path)
  123. {
  124. phpCAS::traceBegin();
  125. // call the ancestor's constructor
  126. $this->PGTStorage($cas_parent);
  127. if (empty($format) ) $format = CAS_PGT_STORAGE_FILE_DEFAULT_FORMAT;
  128. if (empty($path) ) $path = CAS_PGT_STORAGE_FILE_DEFAULT_PATH;
  129. // check that the path is an absolute path
  130. if (getenv("OS")=="Windows_NT"){
  131. if (!preg_match('`^[a-zA-Z]:`', $path)) {
  132. phpCAS::error('an absolute path is needed for PGT storage to file');
  133. }
  134. }
  135. else
  136. {
  137. if ( $path[0] != '/' ) {
  138. phpCAS::error('an absolute path is needed for PGT storage to file');
  139. }
  140. // store the path (with a leading and trailing '/')
  141. $path = preg_replace('|[/]*$|','/',$path);
  142. $path = preg_replace('|^[/]*|','/',$path);
  143. }
  144. $this->_path = $path;
  145. // check the format and store it
  146. switch ($format) {
  147. case CAS_PGT_STORAGE_FILE_FORMAT_PLAIN:
  148. case CAS_PGT_STORAGE_FILE_FORMAT_XML:
  149. $this->_format = $format;
  150. break;
  151. default:
  152. phpCAS::error('unknown PGT file storage format (`'.CAS_PGT_STORAGE_FILE_FORMAT_PLAIN.'\' and `'.CAS_PGT_STORAGE_FILE_FORMAT_XML.'\' allowed)');
  153. }
  154. phpCAS::traceEnd();
  155. }
  156. // ########################################################################
  157. // INITIALIZATION
  158. // ########################################################################
  159. /**
  160. * This method is used to initialize the storage. Halts on error.
  161. *
  162. * @public
  163. */
  164. function init()
  165. {
  166. phpCAS::traceBegin();
  167. // if the storage has already been initialized, return immediatly
  168. if ( $this->isInitialized() )
  169. return;
  170. // call the ancestor's method (mark as initialized)
  171. parent::init();
  172. phpCAS::traceEnd();
  173. }
  174. // ########################################################################
  175. // PGT I/O
  176. // ########################################################################
  177. /**
  178. * This method returns the filename corresponding to a PGT Iou.
  179. *
  180. * @param $pgt_iou the PGT iou.
  181. *
  182. * @return a filename
  183. * @private
  184. */
  185. function getPGTIouFilename($pgt_iou)
  186. {
  187. phpCAS::traceBegin();
  188. $filename = $this->getPath().$pgt_iou.'.'.$this->getFormat();
  189. phpCAS::traceEnd($filename);
  190. return $filename;
  191. }
  192. /**
  193. * This method stores a PGT and its corresponding PGT Iou into a file. Echoes a
  194. * warning on error.
  195. *
  196. * @param $pgt the PGT
  197. * @param $pgt_iou the PGT iou
  198. *
  199. * @public
  200. */
  201. function write($pgt,$pgt_iou)
  202. {
  203. phpCAS::traceBegin();
  204. $fname = $this->getPGTIouFilename($pgt_iou);
  205. if ( $f=fopen($fname,"w") ) {
  206. if ( fputs($f,$pgt) === FALSE ) {
  207. phpCAS::error('could not write PGT to `'.$fname.'\'');
  208. }
  209. fclose($f);
  210. } else {
  211. phpCAS::error('could not open `'.$fname.'\'');
  212. }
  213. phpCAS::traceEnd();
  214. }
  215. /**
  216. * This method reads a PGT corresponding to a PGT Iou and deletes the
  217. * corresponding file.
  218. *
  219. * @param $pgt_iou the PGT iou
  220. *
  221. * @return the corresponding PGT, or FALSE on error
  222. *
  223. * @public
  224. */
  225. function read($pgt_iou)
  226. {
  227. phpCAS::traceBegin();
  228. $pgt = FALSE;
  229. $fname = $this->getPGTIouFilename($pgt_iou);
  230. if ( !($f=fopen($fname,"r")) ) {
  231. phpCAS::trace('could not open `'.$fname.'\'');
  232. } else {
  233. if ( ($pgt=fgets($f)) === FALSE ) {
  234. phpCAS::trace('could not read PGT from `'.$fname.'\'');
  235. }
  236. fclose($f);
  237. }
  238. // delete the PGT file
  239. @unlink($fname);
  240. phpCAS::traceEnd($pgt);
  241. return $pgt;
  242. }
  243. /** @} */
  244. }
  245. ?>