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

/auth/cas/CAS/vendor/apereo/phpcas/source/CAS/PGTStorage/File.php

https://bitbucket.org/moodle/moodle
PHP | 261 lines | 99 code | 24 blank | 138 comment | 19 complexity | 3da4f64e1c72199eb09fc326dead7303 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. /**
  3. * Licensed to Jasig under one or more contributor license
  4. * agreements. See the NOTICE file distributed with this work for
  5. * additional information regarding copyright ownership.
  6. *
  7. * Jasig licenses this file to you under the Apache License,
  8. * Version 2.0 (the "License"); you may not use this file except in
  9. * compliance with the License. You may obtain a copy of the License at:
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * PHP Version 5
  20. *
  21. * @file CAS/PGTStorage/AbstractStorage.php
  22. * @category Authentication
  23. * @package PhpCAS
  24. * @author Pascal Aubry <pascal.aubry@univ-rennes1.fr>
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  26. * @link https://wiki.jasig.org/display/CASC/phpCAS
  27. */
  28. /**
  29. * The CAS_PGTStorage_File class is a class for PGT file storage. An instance of
  30. * this class is returned by CAS_Client::SetPGTStorageFile().
  31. *
  32. * @class CAS_PGTStorage_File
  33. * @category Authentication
  34. * @package PhpCAS
  35. * @author Pascal Aubry <pascal.aubry@univ-rennes1.fr>
  36. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  37. * @link https://wiki.jasig.org/display/CASC/phpCAS
  38. *
  39. *
  40. * @ingroup internalPGTStorageFile
  41. */
  42. class CAS_PGTStorage_File extends CAS_PGTStorage_AbstractStorage
  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 string the name of a directory (with leading and trailing '/')
  60. *
  61. * @private
  62. */
  63. function getPath()
  64. {
  65. return $this->_path;
  66. }
  67. // ########################################################################
  68. // DEBUGGING
  69. // ########################################################################
  70. /**
  71. * This method returns an informational string giving the type of storage
  72. * used by the object (used for debugging purposes).
  73. *
  74. * @return string an informational string.
  75. * @public
  76. */
  77. function getStorageType()
  78. {
  79. return "file";
  80. }
  81. /**
  82. * This method returns an informational string giving informations on the
  83. * parameters of the storage.(used for debugging purposes).
  84. *
  85. * @return string an informational string.
  86. * @public
  87. */
  88. function getStorageInfo()
  89. {
  90. return 'path=`'.$this->getPath().'\'';
  91. }
  92. // ########################################################################
  93. // CONSTRUCTOR
  94. // ########################################################################
  95. /**
  96. * The class constructor, called by CAS_Client::SetPGTStorageFile().
  97. *
  98. * @param CAS_Client $cas_parent the CAS_Client instance that creates the object.
  99. * @param string $path the path where the PGT's should be stored
  100. *
  101. * @return void
  102. *
  103. * @public
  104. */
  105. function __construct($cas_parent,$path)
  106. {
  107. phpCAS::traceBegin();
  108. // call the ancestor's constructor
  109. parent::__construct($cas_parent);
  110. if (empty($path)) {
  111. $path = CAS_PGT_STORAGE_FILE_DEFAULT_PATH;
  112. }
  113. // check that the path is an absolute path
  114. if (getenv("OS")=="Windows_NT" || strtoupper(substr(PHP_OS,0,3)) == 'WIN') {
  115. if (!preg_match('`^[a-zA-Z]:`', $path)) {
  116. phpCAS::error('an absolute path is needed for PGT storage to file');
  117. }
  118. } else {
  119. if ( $path[0] != '/' ) {
  120. phpCAS::error('an absolute path is needed for PGT storage to file');
  121. }
  122. // store the path (with a leading and trailing '/')
  123. $path = preg_replace('|[/]*$|', '/', $path);
  124. $path = preg_replace('|^[/]*|', '/', $path);
  125. }
  126. $this->_path = $path;
  127. phpCAS::traceEnd();
  128. }
  129. // ########################################################################
  130. // INITIALIZATION
  131. // ########################################################################
  132. /**
  133. * This method is used to initialize the storage. Halts on error.
  134. *
  135. * @return void
  136. * @public
  137. */
  138. function init()
  139. {
  140. phpCAS::traceBegin();
  141. // if the storage has already been initialized, return immediatly
  142. if ($this->isInitialized()) {
  143. return;
  144. }
  145. // call the ancestor's method (mark as initialized)
  146. parent::init();
  147. phpCAS::traceEnd();
  148. }
  149. // ########################################################################
  150. // PGT I/O
  151. // ########################################################################
  152. /**
  153. * This method returns the filename corresponding to a PGT Iou.
  154. *
  155. * @param string $pgt_iou the PGT iou.
  156. *
  157. * @return string a filename
  158. * @private
  159. */
  160. function getPGTIouFilename($pgt_iou)
  161. {
  162. phpCAS::traceBegin();
  163. $filename = $this->getPath()."phpcas-".hash("sha256", $pgt_iou);
  164. // $filename = $this->getPath().$pgt_iou.'.plain';
  165. phpCAS::trace("Sha256 filename:" . $filename);
  166. phpCAS::traceEnd();
  167. return $filename;
  168. }
  169. /**
  170. * This method stores a PGT and its corresponding PGT Iou into a file. Echoes a
  171. * warning on error.
  172. *
  173. * @param string $pgt the PGT
  174. * @param string $pgt_iou the PGT iou
  175. *
  176. * @return void
  177. *
  178. * @public
  179. */
  180. function write($pgt,$pgt_iou)
  181. {
  182. phpCAS::traceBegin();
  183. $fname = $this->getPGTIouFilename($pgt_iou);
  184. if (!file_exists($fname)) {
  185. touch($fname);
  186. // Chmod will fail on windows
  187. @chmod($fname, 0600);
  188. if ($f=fopen($fname, "w")) {
  189. if (fputs($f, $pgt) === false) {
  190. phpCAS::error('could not write PGT to `'.$fname.'\'');
  191. }
  192. phpCAS::trace('Successful write of PGT to `'.$fname.'\'');
  193. fclose($f);
  194. } else {
  195. phpCAS::error('could not open `'.$fname.'\'');
  196. }
  197. } else {
  198. phpCAS::error('File exists: `'.$fname.'\'');
  199. }
  200. phpCAS::traceEnd();
  201. }
  202. /**
  203. * This method reads a PGT corresponding to a PGT Iou and deletes the
  204. * corresponding file.
  205. *
  206. * @param string $pgt_iou the PGT iou
  207. *
  208. * @return string|false the corresponding PGT, or FALSE on error
  209. *
  210. * @public
  211. */
  212. function read($pgt_iou)
  213. {
  214. phpCAS::traceBegin();
  215. $pgt = false;
  216. $fname = $this->getPGTIouFilename($pgt_iou);
  217. if (file_exists($fname)) {
  218. if (!($f=fopen($fname, "r"))) {
  219. phpCAS::error('could not open `'.$fname.'\'');
  220. } else {
  221. if (($pgt=fgets($f)) === false) {
  222. phpCAS::error('could not read PGT from `'.$fname.'\'');
  223. }
  224. phpCAS::trace('Successful read of PGT to `'.$fname.'\'');
  225. fclose($f);
  226. }
  227. // delete the PGT file
  228. @unlink($fname);
  229. } else {
  230. phpCAS::error('No such file `'.$fname.'\'');
  231. }
  232. phpCAS::traceEnd($pgt);
  233. return $pgt;
  234. }
  235. /** @} */
  236. }
  237. ?>