PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/auth/cas/CAS/CAS/PGTStorage/pgt-file.php

https://github.com/nadavkav/Moodle-RTL--Shenkar-Translation-Team-
PHP | 249 lines | 99 code | 31 blank | 119 comment | 14 complexity | 0b3da1b6fb6cd37819ce9c94ee3422fd MD5 | raw file
  1. <?php
  2. /**
  3. * @file CAS/PGTStorage/pgt-file.php
  4. * Basic class for PGT file storage
  5. */
  6. /**
  7. * @class PGTStorageFile
  8. * The PGTStorageFile class is a class for PGT file storage. An instance of
  9. * this class is returned by CASClient::SetPGTStorageFile().
  10. *
  11. * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr>
  12. *
  13. * @ingroup internalPGTStorageFile
  14. */
  15. class PGTStorageFile extends PGTStorage
  16. {
  17. /**
  18. * @addtogroup internalPGTStorageFile
  19. * @{
  20. */
  21. /**
  22. * a string telling where PGT's should be stored on the filesystem. Written by
  23. * PGTStorageFile::PGTStorageFile(), read by getPath().
  24. *
  25. * @private
  26. */
  27. var $_path;
  28. /**
  29. * This method returns the name of the directory where PGT's should be stored
  30. * on the filesystem.
  31. *
  32. * @return the name of a directory (with leading and trailing '/')
  33. *
  34. * @private
  35. */
  36. function getPath()
  37. {
  38. return $this->_path;
  39. }
  40. /**
  41. * a string telling the format to use to store PGT's (plain or xml). Written by
  42. * PGTStorageFile::PGTStorageFile(), read by getFormat().
  43. *
  44. * @private
  45. */
  46. var $_format;
  47. /**
  48. * This method returns the format to use when storing PGT's on the filesystem.
  49. *
  50. * @return a string corresponding to the format used (plain or xml).
  51. *
  52. * @private
  53. */
  54. function getFormat()
  55. {
  56. return $this->_format;
  57. }
  58. // ########################################################################
  59. // DEBUGGING
  60. // ########################################################################
  61. /**
  62. * This method returns an informational string giving the type of storage
  63. * used by the object (used for debugging purposes).
  64. *
  65. * @return an informational string.
  66. * @public
  67. */
  68. function getStorageType()
  69. {
  70. return "file";
  71. }
  72. /**
  73. * This method returns an informational string giving informations on the
  74. * parameters of the storage.(used for debugging purposes).
  75. *
  76. * @return an informational string.
  77. * @public
  78. */
  79. function getStorageInfo()
  80. {
  81. return 'path=`'.$this->getPath().'\', format=`'.$this->getFormat().'\'';
  82. }
  83. // ########################################################################
  84. // CONSTRUCTOR
  85. // ########################################################################
  86. /**
  87. * The class constructor, called by CASClient::SetPGTStorageFile().
  88. *
  89. * @param $cas_parent the CASClient instance that creates the object.
  90. * @param $format the format used to store the PGT's (`plain' and `xml' allowed).
  91. * @param $path the path where the PGT's should be stored
  92. *
  93. * @public
  94. */
  95. function PGTStorageFile($cas_parent,$format,$path)
  96. {
  97. phpCAS::traceBegin();
  98. // call the ancestor's constructor
  99. $this->PGTStorage($cas_parent);
  100. if (empty($format) ) $format = CAS_PGT_STORAGE_FILE_DEFAULT_FORMAT;
  101. if (empty($path) ) $path = CAS_PGT_STORAGE_FILE_DEFAULT_PATH;
  102. // check that the path is an absolute path
  103. if (getenv("OS")=="Windows_NT"){
  104. if (!preg_match('`^[a-zA-Z]:`', $path)) {
  105. phpCAS::error('an absolute path is needed for PGT storage to file');
  106. }
  107. }
  108. else
  109. {
  110. if ( $path[0] != '/' ) {
  111. phpCAS::error('an absolute path is needed for PGT storage to file');
  112. }
  113. // store the path (with a leading and trailing '/')
  114. $path = preg_replace('|[/]*$|','/',$path);
  115. $path = preg_replace('|^[/]*|','/',$path);
  116. }
  117. $this->_path = $path;
  118. // check the format and store it
  119. switch ($format) {
  120. case CAS_PGT_STORAGE_FILE_FORMAT_PLAIN:
  121. case CAS_PGT_STORAGE_FILE_FORMAT_XML:
  122. $this->_format = $format;
  123. break;
  124. default:
  125. phpCAS::error('unknown PGT file storage format (`'.CAS_PGT_STORAGE_FILE_FORMAT_PLAIN.'\' and `'.CAS_PGT_STORAGE_FILE_FORMAT_XML.'\' allowed)');
  126. }
  127. phpCAS::traceEnd();
  128. }
  129. // ########################################################################
  130. // INITIALIZATION
  131. // ########################################################################
  132. /**
  133. * This method is used to initialize the storage. Halts on error.
  134. *
  135. * @public
  136. */
  137. function init()
  138. {
  139. phpCAS::traceBegin();
  140. // if the storage has already been initialized, return immediatly
  141. if ( $this->isInitialized() )
  142. return;
  143. // call the ancestor's method (mark as initialized)
  144. parent::init();
  145. phpCAS::traceEnd();
  146. }
  147. // ########################################################################
  148. // PGT I/O
  149. // ########################################################################
  150. /**
  151. * This method returns the filename corresponding to a PGT Iou.
  152. *
  153. * @param $pgt_iou the PGT iou.
  154. *
  155. * @return a filename
  156. * @private
  157. */
  158. function getPGTIouFilename($pgt_iou)
  159. {
  160. phpCAS::traceBegin();
  161. $filename = $this->getPath().$pgt_iou.'.'.$this->getFormat();
  162. phpCAS::traceEnd($filename);
  163. return $filename;
  164. }
  165. /**
  166. * This method stores a PGT and its corresponding PGT Iou into a file. Echoes a
  167. * warning on error.
  168. *
  169. * @param $pgt the PGT
  170. * @param $pgt_iou the PGT iou
  171. *
  172. * @public
  173. */
  174. function write($pgt,$pgt_iou)
  175. {
  176. phpCAS::traceBegin();
  177. $fname = $this->getPGTIouFilename($pgt_iou);
  178. if ( $f=fopen($fname,"w") ) {
  179. if ( fputs($f,$pgt) === FALSE ) {
  180. phpCAS::error('could not write PGT to `'.$fname.'\'');
  181. }
  182. fclose($f);
  183. } else {
  184. phpCAS::error('could not open `'.$fname.'\'');
  185. }
  186. phpCAS::traceEnd();
  187. }
  188. /**
  189. * This method reads a PGT corresponding to a PGT Iou and deletes the
  190. * corresponding file.
  191. *
  192. * @param $pgt_iou the PGT iou
  193. *
  194. * @return the corresponding PGT, or FALSE on error
  195. *
  196. * @public
  197. */
  198. function read($pgt_iou)
  199. {
  200. phpCAS::traceBegin();
  201. $pgt = FALSE;
  202. $fname = $this->getPGTIouFilename($pgt_iou);
  203. if ( !($f=fopen($fname,"r")) ) {
  204. phpCAS::trace('could not open `'.$fname.'\'');
  205. } else {
  206. if ( ($pgt=fgets($f)) === FALSE ) {
  207. phpCAS::trace('could not read PGT from `'.$fname.'\'');
  208. }
  209. fclose($f);
  210. }
  211. // delete the PGT file
  212. @unlink($fname);
  213. phpCAS::traceEnd($pgt);
  214. return $pgt;
  215. }
  216. /** @} */
  217. }
  218. ?>