/usr/share/php/Horde/Lock/Sql.php is in php-horde-lock 2.1.4-1.
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | <?php
/**
* The Horde_Lock_Sql driver implements a storage backend for the Horde_Lock
* API.
*
* The table structure for the locks is as follows:
* <pre>
* CREATE TABLE horde_locks (
* lock_id VARCHAR(36) NOT NULL,
* lock_owner VARCHAR(32) NOT NULL,
* lock_scope VARCHAR(32) NOT NULL,
* lock_principal VARCHAR(255) NOT NULL,
* lock_origin_timestamp BIGINT NOT NULL,
* lock_update_timestamp BIGINT NOT NULL,
* lock_expiry_timestamp BIGINT NOT NULL,
* lock_type TINYINT NOT NULL,
*
* PRIMARY KEY (lock_id)
* );
* </pre>
*
* Copyright 2008-2016 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 Ben Klang <bklang@horde.org>
* @category Horde
* @package Lock
*/
class Horde_Lock_Sql extends Horde_Lock
{
/**
* Handle for the current database connection.
*
* @var Horde_Db_Adapter
*/
private $_db;
/**
* Constructor.
*
* @param array $params Parameters:
* <pre>
* 'db' - (Horde_Db_Adapter) [REQUIRED] The DB instance.
* 'table' - (string) The name of the lock table in 'database'.
* DEFAULT: 'horde_locks'
* </pre>
*
* @throws Horde_Lock_Exception
*/
public function __construct($params = array())
{
if (!isset($params['db'])) {
throw new Horde_Lock_Exception('Missing db parameter.');
}
$this->_db = $params['db'];
unset($params['db']);
$params = array_merge(array(
'table' => 'horde_locks'
), $params);
parent::__construct($params);
/* Only do garbage collection 0.1% of the time we create an object. */
if (substr(time(), -3) === '000') {
register_shutdown_function(array($this, 'doGC'));
}
}
/**
* Return an array of information about the requested lock.
*
* @see Horde_Lock_Base::getLockInfo()
*/
public function getLockInfo($lockid)
{
$now = time();
$sql = 'SELECT lock_id, lock_owner, lock_scope, lock_principal, '
. 'lock_origin_timestamp, lock_update_timestamp, '
. 'lock_expiry_timestamp, lock_type FROM '
. $this->_params['table']
. ' WHERE lock_id = ? AND '
. '(lock_expiry_timestamp >= ? OR lock_expiry_timestamp = ?)';
$values = array($lockid, $now, Horde_Lock::PERMANENT);
try {
return $this->_db->selectOne($sql, $values);
} catch (Horde_Db_Exception $e) {
throw new Horde_Lock_Exception($e);
}
}
/**
* Return a list of valid locks with the option to limit the results
* by principal, scope and/or type.
*
* @see Horde_Lock_Base::getLocks()
*/
public function getLocks($scope = null, $principal = null, $type = null)
{
$now = time();
$sql = 'SELECT lock_id, lock_owner, lock_scope, lock_principal, '
. 'lock_origin_timestamp, lock_update_timestamp, '
. 'lock_expiry_timestamp, lock_type FROM '
. $this->_params['table']
. ' WHERE (lock_expiry_timestamp >= ? OR lock_expiry_timestamp = ?)';
$values = array($now, Horde_Lock::PERMANENT);
// Check to see if we need to filter the results
if (!empty($principal)) {
$sql .= ' AND lock_principal = ?';
$values[] = $principal;
}
if (!empty($scope)) {
$sql .= ' AND lock_scope = ?';
$values[] = $scope;
}
if (!empty($type)) {
$sql .= ' AND lock_type = ?';
$values[] = $type;
}
try {
$result = $this->_db->select($sql, $values);
} catch (Horde_Db_Exception $e) {
throw new Horde_Lock_Exception($e);
}
$locks = array();
foreach ($result as $row) {
$locks[$row['lock_id']] = $row;
}
return $locks;
}
/**
* Extend the valid lifetime of a valid lock to now + $lifetime.
*
* @see Horde_Lock_Base::resetLock()
*/
public function resetLock($lockid, $lifetime)
{
$now = time();
if (!$this->getLockInfo($lockid)) {
return false;
}
$expiration = $lifetime == Horde_Lock::PERMANENT ? Horde_Lock::PERMANENT : $now + $lifetime;
$sql = 'UPDATE ' . $this->_params['table'] . ' SET ' .
'lock_update_timestamp = ?, lock_expiry_timestamp = ? ' .
'WHERE lock_id = ? AND lock_expiry_timestamp <> ?';
$values = array($now, $expiration, $lockid, Horde_Lock::PERMANENT);
try {
$this->_db->update($sql, $values);
} catch (Horde_Db_Exception $e) {
throw new Horde_Lock_Exception($e);
}
return true;
}
/**
* Sets a lock on the requested principal and returns the generated lock
* ID.
*
* @see Horde_Lock_Base::setLock()
*/
public function setLock($requestor, $scope, $principal,
$lifetime = 1, $type = Horde_Lock::TYPE_SHARED)
{
$oldlocks = $this->getLocks(
$scope, $principal,
$type == Horde_Lock::TYPE_SHARED ? Horde_Lock::TYPE_EXCLUSIVE : null);
if (count($oldlocks) != 0) {
// A lock exists. Deny the new request.
if ($this->_logger) {
$this->_logger->log(sprintf('Lock requested for %s denied due to existing lock.', $principal), 'NOTICE');
}
return false;
}
$lockid = (string)new Horde_Support_Uuid();
$now = time();
$expiration = $lifetime == Horde_Lock::PERMANENT ? Horde_Lock::PERMANENT : $now + $lifetime;
$sql = 'INSERT INTO ' . $this->_params['table'] . ' (lock_id, lock_owner, lock_scope, lock_principal, lock_origin_timestamp, lock_update_timestamp, lock_expiry_timestamp, lock_type) VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
$values = array($lockid, $requestor, $scope, $principal, $now, $now,
$expiration, $type);
try {
$this->_db->insert($sql, $values);
} catch (Horde_Db_Exception $e) {
throw new Horde_Lock_Exception($e);
}
if ($this->_logger) {
$this->_logger->log(sprintf('Lock %s set successfully by %s in scope %s on "%s"', $lockid, $requestor, $scope, $principal), 'DEBUG');
}
return $lockid;
}
/**
* Removes a lock given the lock ID.
*
* @see Horde_Lock_Base::clearLock()
*/
public function clearLock($lockid)
{
if (empty($lockid)) {
throw new Horde_Lock_Exception('Must supply a valid lock ID.');
}
// Since we're trying to clear the lock we don't care
// whether it is still valid or not. Unconditionally
// remove it.
$sql = 'DELETE FROM ' . $this->_params['table'] . ' WHERE lock_id = ?';
$values = array($lockid);
try {
$this->_db->delete($sql, $values);
} catch (Horde_Db_Exception $e) {
throw new Horde_Lock_Exception($e);
}
if ($this->_logger) {
$this->_logger->log(sprintf('Lock %s cleared successfully.', $lockid), 'DEBUG');
}
return true;
}
/**
* Do garbage collection needed for the driver.
*
* @todo Rename to gc().
*/
public function doGC()
{
$now = time();
$query = 'DELETE FROM ' . $this->_params['table'] . ' WHERE ' .
'lock_expiry_timestamp < ? AND lock_expiry_timestamp != ?';
$values = array($now, Horde_Lock::PERMANENT);
try {
if ($this->_db) {
$result = $this->_db->delete($query, $values);
if ($this->_logger) {
$this->_logger->log(sprintf('Lock garbage collection cleared %d locks.', $result), 'DEBUG');
}
}
} catch (Horde_Db_Exception $e) {}
}
}
|