[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/Horde/Token/ -> file.php (source)

   1  <?php
   2  /**
   3   * Token tracking implementation for local files.
   4   *
   5   * Optional parameters:<pre>
   6   *   'token_dir'  The directory where to keep token files.
   7   *   'timeout'    The period (in seconds) after which an id is purged.
   8   *                Defaults to 86400 (i.e. 24 hours).</pre>
   9   *
  10   * $Horde: framework/Token/Token/file.php,v 1.19.6.7 2006/01/01 21:28:39 jan Exp $
  11   *
  12   * Copyright 1999-2006 Max Kalika <max@horde.org>
  13   *
  14   * See the enclosed file COPYING for license information (LGPL). If you
  15   * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  16   *
  17   * @author  Max Kalika <max@horde.org>
  18   * @since   Horde 1.3
  19   * @package Horde_Token
  20   */
  21  class Horde_Token_file extends Horde_Token {
  22  
  23      /**
  24       * Handle for the open file descriptor.
  25       *
  26       * @var resource
  27       */
  28      var $_fd = false;
  29  
  30      /**
  31       * Boolean indicating whether or not we have an open file descriptor.
  32       *
  33       * @var boolean
  34       */
  35      var $_connected = false;
  36  
  37      /**
  38       * Create a new file based token-tracking container.
  39       *
  40       * @param array $params  A hash containing storage parameters.
  41       */
  42      function Horde_Token_file($params = array())
  43      {
  44          $this->_params = $params;
  45  
  46          /* Choose the directory to save the stub files. */
  47          if (!isset($this->_params['token_dir'])) {
  48              $this->_params['token_dir'] = Util::getTempDir();
  49          }
  50  
  51          /* Set timeout to 24 hours if not specified. */
  52          if (!isset($this->_params['timeout'])) {
  53              $this->_params['timeout'] = 86400;
  54          }
  55      }
  56  
  57      /**
  58       * Deletes all expired connection id's from the SQL server.
  59       *
  60       * @return boolean True on success, a PEAR_Error object on failure.
  61       */
  62      function purge()
  63      {
  64          // Make sure we have no open file descriptors before unlinking
  65          // files.
  66          if (!$this->_disconnect()) {
  67              return PEAR::raiseError('Unable to close file descriptors');
  68          }
  69  
  70          /* Build stub file list. */
  71          if (!($dir = opendir($this->_params['token_dir']))) {
  72              return PEAR::raiseError('Unable to open token directory');
  73          }
  74  
  75          /* Find expired stub files */
  76          while (($dirEntry = readdir($dir)) != '') {
  77              if (preg_match('|^conn_\w{8}$|', $dirEntry) && (time() - filemtime($this->_params['token_dir'] . '/' . $dirEntry) >= $this->_params['timeout'])) {
  78                  if (!@unlink($this->_params['token_dir'] . '/' . $dirEntry)) {
  79                      return PEAR::raiseError('Unable to purge token file.');
  80                  }
  81              }
  82          }
  83  
  84          closedir($dir);
  85          return true;
  86      }
  87  
  88      function exists($tokenID)
  89      {
  90          if (is_a(($result = $this->_connect($tokenID)), 'PEAR_Error')) {
  91              return $result;
  92          }
  93  
  94          /* Find already used IDs. */
  95          $fileContents = @file($this->_params['token_dir'] . '/conn_' . $this->encodeRemoteAddress());
  96          if ($fileContents) {
  97              $iMax = count($fileContents);
  98              for ($i = 0; $i < $iMax; $i++) {
  99                  if (chop($fileContents[$i]) == $tokenID) {
 100                      return true;
 101                  }
 102              }
 103          }
 104  
 105          return false;
 106      }
 107  
 108      function add($tokenID)
 109      {
 110          if (is_a(($result = $this->_connect($tokenID)), 'PEAR_Error')) {
 111              return $result;
 112          }
 113  
 114          /* Write the entry. */
 115          fwrite($this->_fd, "$tokenID\n");
 116  
 117          /* Return an error if the update fails, too. */
 118          if (!$this->_disconnect()) {
 119              return PEAR::raiseError('Failed to close token file cleanly.');
 120          }
 121  
 122          return true;
 123      }
 124  
 125      /**
 126       * Opens a file descriptor to a new or existing file.
 127       *
 128       * @return boolean  True on success, a PEAR_Error object on failure.
 129       */
 130      function _connect($tokenID)
 131      {
 132          if (!$this->_connected) {
 133  
 134              // Open a file descriptor to the token stub file.
 135              $this->_fd = @fopen($this->_params['token_dir'] . '/conn_' . $this->encodeRemoteAddress(), 'a');
 136              if (!$this->_fd) {
 137                  return PEAR::raiseError('Failed to open token file.');
 138              }
 139  
 140              $this->_connected = true;
 141          }
 142  
 143          return true;
 144      }
 145  
 146      /**
 147       * Closes the file descriptor.
 148       *
 149       * @return boolean  True on success, false on failure.
 150       */
 151      function _disconnect()
 152      {
 153          if ($this->_connected) {
 154              $this->_connected = false;
 155              return fclose($this->_fd);
 156          }
 157  
 158          return true;
 159      }
 160  
 161  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7