/usr/share/php/Horde/Token/File.php is in php-horde-token 2.0.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 166 | <?php
/**
* Token tracking implementation for local files.
*
* Copyright 1999-2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Max Kalika <max@horde.org>
* @category Horde
* @package Token
*/
class Horde_Token_File extends Horde_Token_Base
{
/* File prefix constant. */
const FILE_PREFIX = 'conn_';
/**
* Handle for the open file descriptor.
*
* @var resource
*/
protected $_fd = false;
/**
* Boolean indicating whether or not we have an open file descriptor.
*
* @var boolean
*/
protected $_connected = false;
/**
* Constructor.
*
* @see Horde_Token_Base::__construct() for more parameters.
*
* @param array $params Optional parameters:
* - token_dir (string): The directory where to keep token files.
* DEFAULT: System temporary directory
*/
public function __construct($params = array())
{
$params = array_merge(array(
'token_dir' => sys_get_temp_dir()
), $params);
parent::__construct($params);
}
/**
* Destructor.
*/
public function __destruct()
{
$this->_disconnect(false);
}
/**
* Delete all expired connection IDs.
*
* @throws Horde_Token_Exception
*/
public function purge()
{
// Make sure we have no open file descriptors before unlinking
// files.
$this->_disconnect();
/* Build stub file list. */
try {
$di = new DirectoryIterator($this->_params['token_dir']);
} catch (UnexpectedValueException $e) {
throw new Horde_Token_Exception('Unable to open token directory');
}
/* Find expired stub files */
foreach ($di as $val) {
if ($val->isFile() &&
(strpos($val, self::FILE_PREFIX) === 0) &&
(time() - $val->getMTime() >= $this->_params['timeout']) &&
!@unlink($val->getPathname())) {
throw new Horde_Token_Exception('Unable to purge token file.');
}
}
}
/**
* Does the token exist?
*
* @param string $tokenID Token ID.
*
* @return boolean True if the token exists.
* @throws Horde_Token_Exception
*/
public function exists($tokenID)
{
$this->_connect();
/* Find already used IDs. */
$token = base64_encode($tokenID);
foreach (file($this->_params['token_dir'] . '/' . self::FILE_PREFIX . $this->_encodeRemoteAddress()) as $val) {
if (rtrim($val) == $token) {
return true;
}
}
return false;
}
/**
* Add a token ID.
*
* @param string $tokenID Token ID to add.
*
* @throws Horde_Token_Exception
*/
public function add($tokenID)
{
$this->_connect();
/* Write the entry. */
$token = base64_encode($tokenID);
fwrite($this->_fd, $token . "\n");
$this->_disconnect();
}
/**
* Opens a file descriptor to a new or existing file.
*
* @throws Horde_Token_Exception
*/
protected function _connect()
{
if ($this->_connected) {
return;
}
// Open a file descriptor to the token stub file.
$this->_fd = @fopen($this->_params['token_dir'] . '/' . self::FILE_PREFIX . $this->_encodeRemoteAddress(), 'a');
if (!$this->_fd) {
throw new Horde_Token_Exception('Failed to open token file.');
}
$this->_connected = true;
}
/**
* Closes the file descriptor.
*
* @param boolean $error Throw exception on error?
*
* @throws Horde_Token_Exception
*/
protected function _disconnect($error = true)
{
if ($this->_connected) {
$this->_connected = false;
if (!fclose($this->_fd) && $error) {
throw new Horde_Token_Exception('Unable to close file descriptors');
}
}
}
}
|