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