Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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 7
  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  
  45  class 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      var $_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 string the name of a directory (with leading and trailing '/')
  65       *
  66       * @private
  67       */
  68      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 string an informational string.
  82       * @public
  83       */
  84      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 string an informational string.
  94       * @public
  95       */
  96      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      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" || 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;
 143          phpCAS::traceEnd();
 144      }
 145  
 146      // ########################################################################
 147      //  INITIALIZATION
 148      // ########################################################################
 149  
 150      /**
 151       * This method is used to initialize the storage. Halts on error.
 152       *
 153       * @return void
 154       * @public
 155       */
 156      function init()
 157      {
 158          phpCAS::traceBegin();
 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();
 165          phpCAS::traceEnd();
 166      }
 167  
 168      // ########################################################################
 169      //  PGT I/O
 170      // ########################################################################
 171  
 172      /**
 173       * This method returns the filename corresponding to a PGT Iou.
 174       *
 175       * @param string $pgt_iou the PGT iou.
 176       *
 177       * @return string a filename
 178       * @private
 179       */
 180      function getPGTIouFilename($pgt_iou)
 181      {
 182          phpCAS::traceBegin();
 183          $filename = $this->getPath()."phpcas-".hash("sha256", $pgt_iou);
 184  //        $filename = $this->getPath().$pgt_iou.'.plain';
 185          phpCAS::trace("Sha256 filename:" . $filename);
 186          phpCAS::traceEnd();
 187          return $filename;
 188      }
 189  
 190      /**
 191       * This method stores a PGT and its corresponding PGT Iou into a file. Echoes a
 192       * warning on error.
 193       *
 194       * @param string $pgt     the PGT
 195       * @param string $pgt_iou the PGT iou
 196       *
 197       * @return void
 198       *
 199       * @public
 200       */
 201      function write($pgt,$pgt_iou)
 202      {
 203          phpCAS::traceBegin();
 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          }
 221          phpCAS::traceEnd();
 222      }
 223  
 224      /**
 225       * This method reads a PGT corresponding to a PGT Iou and deletes the
 226       * corresponding file.
 227       *
 228       * @param string $pgt_iou the PGT iou
 229       *
 230       * @return string|false the corresponding PGT, or FALSE on error
 231       *
 232       * @public
 233       */
 234      function read($pgt_iou)
 235      {
 236          phpCAS::traceBegin();
 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  
 258      /** @} */
 259  
 260  }
 261  ?>