[1] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * The RecordLock:: class provides a system for locking abstract DB rows. |
---|
| 4 | * |
---|
| 5 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 6 | * @version 2.0 |
---|
| 7 | */ |
---|
| 8 | class RecordLock { |
---|
| 9 | |
---|
| 10 | // Configuration of this object. |
---|
| 11 | var $_params = array( |
---|
| 12 | 'timeout' => 600, |
---|
| 13 | 'auto_timeout' => 1800, |
---|
| 14 | 'error_url' => '/lock.php', |
---|
| 15 | 'db_table' => 'lock_tbl', |
---|
| 16 | 'create_table' => true, // Automatically create table and verify columns. Better set to false after site launch. |
---|
| 17 | ); |
---|
| 18 | |
---|
| 19 | // Store lock data from DB. |
---|
| 20 | var $data = array(); |
---|
| 21 | |
---|
| 22 | // Auth_SQL object from which to access a current user_id. |
---|
| 23 | var $_auth; |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * This method enforces the singleton pattern for this class. |
---|
| 27 | * |
---|
| 28 | * @return object Reference to the global RecordLock object. |
---|
| 29 | * @access public |
---|
| 30 | * @static |
---|
| 31 | */ |
---|
| 32 | function &getInstance($auth_object) |
---|
| 33 | { |
---|
| 34 | static $instances = array(); |
---|
[42] | 35 | |
---|
[1] | 36 | if (!isset($instances[$auth_object->getVal('auth_name')])) { |
---|
| 37 | $instances[$auth_object->getVal('auth_name')] = new RecordLock($auth_object); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | return $instances[$auth_object->getVal('auth_name')]; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | * Constructor. Pass an Auth object on which to perform user lookups. |
---|
| 45 | * |
---|
| 46 | * @param mixed $auth_object An Auth_SQL object. |
---|
| 47 | */ |
---|
| 48 | function RecordLock($auth_object) |
---|
| 49 | { |
---|
[22] | 50 | if (!is_a($auth_object, 'Auth_SQL')) { |
---|
| 51 | trigger_error('Constructor not provided a valid Auth_SQL object.', E_USER_ERROR); |
---|
| 52 | } |
---|
| 53 | |
---|
[1] | 54 | $this->_auth = $auth_object; |
---|
[42] | 55 | |
---|
[1] | 56 | // Get create tables config from global context. |
---|
| 57 | if (!is_null(App::getParam('db_create_tables'))) { |
---|
| 58 | $this->setParam(array('create_table' => App::getParam('db_create_tables'))); |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * Setup the database table for this class. |
---|
| 64 | * |
---|
| 65 | * @access public |
---|
| 66 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 67 | * @since 26 Aug 2005 17:09:36 |
---|
| 68 | */ |
---|
| 69 | function initDB($recreate_db=false) |
---|
| 70 | { |
---|
| 71 | static $_db_tested = false; |
---|
[42] | 72 | |
---|
[1] | 73 | if ($recreate_db || !$_db_tested && $this->getParam('create_table')) { |
---|
| 74 | if ($recreate_db) { |
---|
| 75 | DB::query("DROP TABLE IF EXISTS " . $this->getParam('db_table')); |
---|
| 76 | App::logMsg(sprintf('Dropping and recreating table %s.', $this->getParam('db_table')), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 77 | } |
---|
| 78 | DB::query("CREATE TABLE IF NOT EXISTS " . $this->getParam('db_table') . " ( |
---|
| 79 | lock_id int NOT NULL auto_increment, |
---|
| 80 | record_table varchar(255) NOT NULL default '', |
---|
| 81 | record_key varchar(255) NOT NULL default '', |
---|
| 82 | record_val varchar(255) NOT NULL default '', |
---|
| 83 | title varchar(255) NOT NULL default '', |
---|
| 84 | set_by_admin_id smallint(11) NOT NULL default '0', |
---|
| 85 | lock_datetime datetime NOT NULL default '0000-00-00 00:00:00', |
---|
| 86 | PRIMARY KEY (lock_id), |
---|
| 87 | KEY record_table (record_table), |
---|
| 88 | KEY record_key (record_key), |
---|
| 89 | KEY record_val (record_val) |
---|
| 90 | )"); |
---|
[42] | 91 | |
---|
[1] | 92 | if (!DB::columnExists($this->getParam('db_table'), array( |
---|
[42] | 93 | 'lock_id', |
---|
| 94 | 'record_table', |
---|
| 95 | 'record_key', |
---|
| 96 | 'record_val', |
---|
| 97 | 'title', |
---|
| 98 | 'set_by_admin_id', |
---|
| 99 | 'lock_datetime', |
---|
[1] | 100 | ), false, false)) { |
---|
| 101 | App::logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), LOG_ALERT, __FILE__, __LINE__); |
---|
| 102 | trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), E_USER_ERROR); |
---|
| 103 | } |
---|
[42] | 104 | } |
---|
[1] | 105 | $_db_tested = true; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | * Set the params of this object. |
---|
| 110 | * |
---|
| 111 | * @param array $params Array of param keys and values to set. |
---|
| 112 | */ |
---|
| 113 | function setParam($params=null) |
---|
| 114 | { |
---|
| 115 | if (isset($params) && is_array($params)) { |
---|
| 116 | // Merge new parameters with old overriding only those passed. |
---|
| 117 | $this->_params = array_merge($this->_params, $params); |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | /** |
---|
| 122 | * Return the value of a param setting. |
---|
| 123 | * |
---|
| 124 | * @access public |
---|
| 125 | * @param string $params Which param to return. |
---|
| 126 | * @return mixed Configured param value. |
---|
| 127 | */ |
---|
| 128 | function getParam($param) |
---|
| 129 | { |
---|
| 130 | if (isset($this->_params[$param])) { |
---|
| 131 | return $this->_params[$param]; |
---|
| 132 | } else { |
---|
| 133 | App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 134 | return null; |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | * Select the lock to manipulate. |
---|
| 140 | * |
---|
| 141 | * @param mixed $record_table_or_lock_id The table containing the record to lock, |
---|
| 142 | * or a numeric lock_id. |
---|
| 143 | * @param string $record_key The key column for the record to lock. |
---|
| 144 | * @param string $record_val The value of the key column for the record to lock. |
---|
| 145 | * @param string $title A title to apply to the lock, for display purposes. |
---|
| 146 | */ |
---|
| 147 | function select($record_table_or_lock_id, $record_key=null, $record_val=null) |
---|
| 148 | { |
---|
| 149 | $this->initDB(); |
---|
| 150 | |
---|
| 151 | // Expire old locks. |
---|
| 152 | $this->_auto_timeout(); |
---|
[42] | 153 | |
---|
[1] | 154 | if (is_numeric($record_table_or_lock_id) && !isset($record_key) && !isset($record_val)) { |
---|
| 155 | // Get lock data by lock_id. |
---|
| 156 | $qid = DB::query(" |
---|
[42] | 157 | SELECT * FROM " . $this->getParam('db_table') . " |
---|
[1] | 158 | WHERE lock_id = '" . addslashes($record_table_or_lock_id) . "' |
---|
| 159 | "); |
---|
| 160 | } else { |
---|
| 161 | // Get lock data by record specs |
---|
| 162 | $qid = DB::query(" |
---|
[42] | 163 | SELECT * FROM " . $this->getParam('db_table') . " |
---|
[1] | 164 | WHERE record_table = '" . addslashes($record_table_or_lock_id) . "' |
---|
| 165 | AND record_key = '" . addslashes($record_key) . "' |
---|
| 166 | AND record_val = '" . addslashes($record_val) . "' |
---|
| 167 | "); |
---|
| 168 | } |
---|
| 169 | if ($this->data = mysql_fetch_assoc($qid)) { |
---|
[22] | 170 | App::logMsg(sprintf('Selecting %slocked record: %s %s %s', ($this->data['set_by_admin_id'] == $this->_auth->getVal('user_id') ? 'self-' : ''), $record_table_or_lock_id, $record_key, $record_val), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 171 | /// FIX ME: What if admin set lock, but public user is current lock user? |
---|
[1] | 172 | $this->data['editor'] = $this->_auth->getUsername($this->data['set_by_admin_id']); |
---|
| 173 | return true; |
---|
| 174 | } else { |
---|
| 175 | App::logMsg(sprintf('No locked record: %s %s %s', $record_table_or_lock_id, $record_key, $record_val), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 176 | return false; |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | /** |
---|
| 181 | * Returns true if the record we instantiated with is locked. |
---|
| 182 | * |
---|
| 183 | * @return bool True if locked. |
---|
| 184 | */ |
---|
| 185 | function isLocked() |
---|
| 186 | { |
---|
| 187 | return isset($this->data['lock_id']); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /** |
---|
[42] | 191 | * Returns the status of who set the lock. Use this to ignore locks set by |
---|
[1] | 192 | * the current user. |
---|
| 193 | * |
---|
| 194 | * @return bool True if current user set the lock. |
---|
| 195 | */ |
---|
| 196 | function isMine() |
---|
| 197 | { |
---|
| 198 | $this->initDB(); |
---|
[42] | 199 | |
---|
[1] | 200 | if (isset($this->data['lock_id'])) { |
---|
| 201 | $qid = DB::query("SELECT * FROM " . $this->getParam('db_table') . " WHERE lock_id = '" . addslashes($this->data['lock_id']) . "'"); |
---|
| 202 | if ($lock = mysql_fetch_assoc($qid)) { |
---|
| 203 | return ($lock['set_by_admin_id'] == $this->_auth->getVal('user_id')); |
---|
| 204 | } else { |
---|
| 205 | return false; |
---|
| 206 | } |
---|
| 207 | } else { |
---|
| 208 | return false; |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /** |
---|
| 213 | * Create a new lock for the specified table/key/value. |
---|
| 214 | * |
---|
| 215 | * @param string $record_table The table containing the record to lock. |
---|
| 216 | * @param string $record_key The key column for the record to lock. |
---|
| 217 | * @param string $record_val The value of the key column for the record to lock. |
---|
| 218 | * @param string $title A title to apply to the lock, for display purposes. |
---|
| 219 | * |
---|
| 220 | * @return int The id for the lock (mysql last insert id). |
---|
| 221 | */ |
---|
| 222 | function set($record_table, $record_key, $record_val, $title='') |
---|
[42] | 223 | { |
---|
[1] | 224 | $this->initDB(); |
---|
| 225 | |
---|
| 226 | // Expire old locks. |
---|
| 227 | $this->_auto_timeout(); |
---|
[42] | 228 | |
---|
[1] | 229 | // Remove previous locks if exist. Is this better than using a REPLACE INTO? |
---|
| 230 | DB::query(" |
---|
[42] | 231 | DELETE FROM " . $this->getParam('db_table') . " |
---|
[1] | 232 | WHERE record_table = '" . addslashes($record_table) . "' |
---|
| 233 | AND record_key = '" . addslashes($record_key) . "' |
---|
| 234 | AND record_val = '" . addslashes($record_val) . "' |
---|
| 235 | "); |
---|
[42] | 236 | |
---|
[1] | 237 | // Set new lock. |
---|
| 238 | DB::query(" |
---|
| 239 | INSERT INTO " . $this->getParam('db_table') . " ( |
---|
| 240 | record_table, |
---|
| 241 | record_key, |
---|
| 242 | record_val, |
---|
| 243 | title, |
---|
| 244 | set_by_admin_id, |
---|
| 245 | lock_datetime |
---|
| 246 | ) VALUES ( |
---|
| 247 | '" . addslashes($record_table) . "', |
---|
| 248 | '" . addslashes($record_key) . "', |
---|
| 249 | '" . addslashes($record_val) . "', |
---|
| 250 | '" . addslashes($title) . "', |
---|
| 251 | '" . addslashes($this->_auth->getVal('user_id')) . "', |
---|
| 252 | NOW() |
---|
| 253 | ) |
---|
| 254 | "); |
---|
| 255 | $lock_id = mysql_insert_id(DB::getDBH()); |
---|
[42] | 256 | |
---|
[1] | 257 | // Must register this locked record as the current. |
---|
| 258 | $this->select($lock_id); |
---|
[42] | 259 | |
---|
[1] | 260 | return $lock_id; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | /** |
---|
| 264 | * Unlock the currently selected record. |
---|
| 265 | */ |
---|
| 266 | function remove() |
---|
| 267 | { |
---|
| 268 | $this->initDB(); |
---|
| 269 | |
---|
| 270 | // Expire old locks. |
---|
| 271 | $this->_auto_timeout(); |
---|
[42] | 272 | |
---|
[1] | 273 | // Delete a specific lock. |
---|
| 274 | DB::query(" |
---|
[42] | 275 | DELETE FROM " . $this->getParam('db_table') . " |
---|
[1] | 276 | WHERE lock_id = '" . addslashes($this->data['lock_id']) . "' |
---|
| 277 | "); |
---|
[42] | 278 | |
---|
[22] | 279 | App::logMsg(sprintf('Removing lock: %s', $this->data['lock_id']), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 280 | } |
---|
| 281 | |
---|
| 282 | /** |
---|
| 283 | * Unlock all records, or all records for a specified user. |
---|
| 284 | */ |
---|
| 285 | function removeAll($user_id=null) |
---|
[42] | 286 | { |
---|
[1] | 287 | $this->initDB(); |
---|
| 288 | |
---|
| 289 | // Expire old locks. |
---|
| 290 | $this->_auto_timeout(); |
---|
[42] | 291 | |
---|
[1] | 292 | if (isset($user_id)) { |
---|
| 293 | // Delete specific user's locks. |
---|
| 294 | DB::query("DELETE FROM " . $this->getParam('db_table') . " WHERE set_by_admin_id = '" . addslashes($user_id) . "'"); |
---|
[71] | 295 | App::logMsg(sprintf('Record locks owned by %s %s have been deleted', $this->_auth->getVal('auth_name'), $this->_auth->getUsername($user_id)), LOG_DEBUG, __FILE__, __LINE__); |
---|
[1] | 296 | } else { |
---|
| 297 | // Delete ALL locks. |
---|
| 298 | DB::query("DELETE FROM " . $this->getParam('db_table') . ""); |
---|
| 299 | App::logMsg(sprintf('All record locks deleted by %s %s', $this->_auth->getVal('auth_name'), $this->_auth->getVal('username')), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 300 | } |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | /** |
---|
| 304 | * Delete's all locks that are older than auto_timeout. |
---|
| 305 | */ |
---|
| 306 | function _auto_timeout() |
---|
| 307 | { |
---|
| 308 | static $_timeout_run = false; |
---|
| 309 | |
---|
| 310 | $this->initDB(); |
---|
[42] | 311 | |
---|
[1] | 312 | if (!$_timeout_run) { |
---|
| 313 | // Delete all old locks. |
---|
| 314 | DB::query(" |
---|
[42] | 315 | DELETE FROM " . $this->getParam('db_table') . " |
---|
[1] | 316 | WHERE DATE_ADD(lock_datetime, INTERVAL '" . $this->getParam('auto_timeout') . "' SECOND) < NOW() |
---|
| 317 | "); |
---|
| 318 | $_timeout_run = true; |
---|
| 319 | } |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | /** |
---|
| 323 | * Redirect to record lock error page. |
---|
| 324 | */ |
---|
| 325 | function dieErrorPage() |
---|
| 326 | { |
---|
| 327 | App::dieURL(sprintf('%s?lock_id=%s&boomerang=%s', $this->getParam('error_url'), $this->data['lock_id'], urlencode(absoluteMe()))); |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | /** |
---|
| 331 | * Print error page. |
---|
| 332 | */ |
---|
| 333 | function printErrorHTML() |
---|
| 334 | { |
---|
| 335 | ?> |
---|
| 336 | <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> |
---|
| 337 | <?php App::printHiddenSession() ?> |
---|
| 338 | <input type="hidden" name="lock_id" value="<?php echo $this->getID(); ?>" /> |
---|
[42] | 339 | |
---|
[1] | 340 | <p><?php |
---|
[42] | 341 | printf(_("The record <strong>%s</strong> is currently being edited by <strong>%s</strong> (%d minutes elapsed). You cannot modify the record while it is locked by another user."), |
---|
| 342 | $this->getTitle(), |
---|
| 343 | $this->getEditor(), |
---|
[1] | 344 | date('i', $this->getSecondsElapsed() + 60) |
---|
| 345 | ); |
---|
| 346 | ?></p> |
---|
[42] | 347 | |
---|
[1] | 348 | <?php if ($this->getSecondsElapsed() > $this->getParam('timeout')) { ?> |
---|
| 349 | <p><?php printf(_("You can forcibly unlock the record if you believe the editing session has expired. You might want to confirm with %s before doing this."), $this->getEditor()) ?></p> |
---|
[28] | 350 | <input type="submit" name="unlock" value="<?php echo _("Unlock"); ?>" /> |
---|
[1] | 351 | <?php } ?> |
---|
[42] | 352 | |
---|
[28] | 353 | <input type="submit" name="cancel" value="<?php echo _("Cancel"); ?>" /> |
---|
[1] | 354 | </form> |
---|
| 355 | <?php |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | /** |
---|
| 359 | * Return lock_id of locked record. |
---|
| 360 | */ |
---|
| 361 | function getID() |
---|
| 362 | { |
---|
| 363 | return $this->data['lock_id']; |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | /** |
---|
| 367 | * Return title of locked record. |
---|
| 368 | */ |
---|
| 369 | function getTitle() |
---|
| 370 | { |
---|
| 371 | return $this->data['title']; |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | /** |
---|
| 375 | * Return administrator username for locked record. |
---|
| 376 | */ |
---|
| 377 | function getEditor() |
---|
| 378 | { |
---|
| 379 | return $this->data['editor']; |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | /** |
---|
| 383 | * Return total seconds since the record was locked. |
---|
| 384 | */ |
---|
| 385 | function getSecondsElapsed() |
---|
| 386 | { |
---|
| 387 | if (isset($this->data['lock_datetime']) && $this->data['lock_datetime'] < time()) { |
---|
| 388 | return time() - strtotime($this->data['lock_datetime']); |
---|
| 389 | } else { |
---|
| 390 | return 0; |
---|
| 391 | } |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | |
---|
| 395 | } // End of class. |
---|
| 396 | ?> |
---|