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 162 163 164 165
|
<?php
/*
* $Horde: horde/lib/Token/file.php,v 1.4.2.3 2003/01/03 12:48:43 jan Exp $
*
* Copyright 1999-2003 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.
*/
/**
* Token tracking implementation for stub files.
*
* Optional values for $params:
* 'stub_dir' The directory where to keep connection stub files.
* 'timeout' The period (in seconds) after which an id is purged.
*
* @author Max Kalika <max@horde.org>
* @version $Revision: 1.4.2.3 $
* @since Horde 1.3
* @package horde.token
*/
class Token_file extends Token {
/** Handle for the open file descriptor. */
var $fd = '';
/** Boolean indicating whether or not we have an open file descriptor. */
var $connected = false;
/**
* Create a new file based tracking storage container.
*
* @param optional array $params A hash containing storage parameters.
*/
function Token_file($params = array())
{
$this->params = $params;
/* Choose the directory to save the stub files. */
if (!isset($this->params['stub_dir'])) {
if (ini_get('upload_tmp_dir')) {
$this->params['stub_dir'] = ini_get('upload_tmp_dir');
}
elseif (getenv('TMPDIR')) {
$this->params['stub_dir'] = getenv('TMPDIR');
}
else {
$this->params['stub_dir'] = '/tmp';
}
}
/* Set timeout to 24 hours if not specified. */
if (!isset($this->params['timeout'])) {
$this->params['timeout'] = 86400;
}
}
/**
* Opens a file descriptor to a new or existing file.
*
* @return bool TOKEN_OK on success, TOKEN_ERROR_* on failure.
*/
function connect()
{
if (!$this->connected) {
/*
* Open a file descriptor to the connection stub file.
*/
$this->fd = fopen($this->params['stub_dir'] . "/conn_" . $this->hexRemoteAddr(), "a");
if (!$this->fd) {
return TOKEN_ERROR_CONNECT;
}
$this->connected = true;
}
return TOKEN_OK;
}
/**
* Closes the file descriptor.
*
* @return bool true on success, false on failure.
*/
function disconnect()
{
if ($this->connected) {
$this->connected = false;
return fclose($this->fd);
}
return true;
}
/**
* Deletes all expired connection id's from the SQL server.
*
* @return bool TOKEN_OK on success, TOKEN_ERROR_* on failure.
*/
function purge()
{
/* If we're already connected, disconnect as we will be unlinking files. */
if ($this->connected) {
if ($this->disconnect()) return TOKEN_ERROR_CONNECT;
}
/* Build stub file list. */
if (!$dirFD = opendir($this->params['stub_dir'])) {
return TOKEN_ERROR_CONNECT;
}
/* Find expired stub files */
while (($dirEntry = readdir($dirFD)) != '') {
if (preg_match('|^conn_\w{8}$|', $dirEntry) && (time() - filemtime($this->params['stub_dir'] . "/" . $dirEntry) >= $this->params['timeout'])) {
if (!unlink($this->params['stub_dir'] . "/" . $dirEntry)) {
return TOKEN_ERROR;
}
}
}
closedir($dirFD);
return TOKEN_OK;
}
function exists($connID)
{
/* If we're already connected, disconnect as we will be reading one file. */
if ($this->connected) {
if ($this->disconnect()) return TOKEN_ERROR_CONNECT;
}
/* Find already used IDs */
$fileContents = @file($this->params['stub_dir'] . '/conn_' . $this->hexRemoteAddr());
if ($fileContents) {
for ($i = 0; $i < count($fileContents); $i++) {
if (chop($fileContents[$i]) == $connID) {
return true;
}
}
}
return false;
}
function add($connID)
{
/* If we're not already connected, invoke the connect(). */
if (!$this->connected) {
if ($this->connect() != TOKEN_OK) return TOKEN_ERROR_CONNECT;
}
/* Write the entry. */
fwrite($this->fd, "$connID\n");
/* Return an error if the update fails, too. */
if (!$this->disconnect()) return TOKEN_ERROR;
return TOKEN_OK;
}
}
?>
|