Changeset 136 for trunk/lib/RecordLock.inc.php
- Timestamp:
- Jun 3, 2006 7:47:48 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/RecordLock.inc.php
r111 r136 1 1 <?php 2 2 /** 3 * The RecordLock:: class provides a system for locking abstract DB rows. 3 * RecordLock.inc.php 4 * code by strangecode :: www.strangecode.com :: this document contains copyrighted information 5 * 6 * The RecordLock class provides a system for locking abstract DB rows. 4 7 * 5 8 * @author Quinn Comendant <quinn@strangecode.com> 6 * @version 2. 09 * @version 2.1 7 10 */ 8 11 class RecordLock { … … 32 35 function &getInstance($auth_object) 33 36 { 34 static $instance s = array();35 36 if ( !isset($instances[$auth_object->getVal('auth_name')])) {37 $instance s[$auth_object->getVal('auth_name')]= new RecordLock($auth_object);38 } 39 40 return $instance s[$auth_object->getVal('auth_name')];37 static $instance = null; 38 39 if ($instance === null) { 40 $instance = new RecordLock($auth_object); 41 } 42 43 return $instance; 41 44 } 42 45 … … 44 47 * Constructor. Pass an Auth object on which to perform user lookups. 45 48 * 46 * @param mixed $auth_object An Auth_SQL o bject.49 * @param mixed $auth_object An Auth_SQL or Auth_FILE object. 47 50 */ 48 51 function RecordLock($auth_object) 49 52 { 50 if (!is_a($auth_object, 'Auth_SQL')) { 51 trigger_error('Constructor not provided a valid Auth_SQL object.', E_USER_ERROR); 53 $app =& App::getInstance(); 54 55 if (!method_exists($auth_object, 'getVal') || !method_exists($auth_object, 'getUsername')) { 56 trigger_error('Constructor not provided a valid Auth_* object.', E_USER_ERROR); 52 57 } 53 58 … … 55 60 56 61 // 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')));62 if (!is_null($app->getParam('db_create_tables'))) { 63 $this->setParam(array('create_table' => $app->getParam('db_create_tables'))); 59 64 } 60 65 } … … 69 74 function initDB($recreate_db=false) 70 75 { 76 $app =& App::getInstance(); 77 $db =& DB::getInstance(); 78 71 79 static $_db_tested = false; 72 80 73 81 if ($recreate_db || !$_db_tested && $this->getParam('create_table')) { 74 82 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__);83 $db->query("DROP TABLE IF EXISTS " . $this->getParam('db_table')); 84 $app->logMsg(sprintf('Dropping and recreating table %s.', $this->getParam('db_table')), LOG_DEBUG, __FILE__, __LINE__); 77 85 } 78 DB::query("CREATE TABLE IF NOT EXISTS " . $this->getParam('db_table') . " (86 $db->query("CREATE TABLE IF NOT EXISTS " . $this->getParam('db_table') . " ( 79 87 lock_id int NOT NULL auto_increment, 80 88 record_table varchar(255) NOT NULL default '', … … 90 98 )"); 91 99 92 if (! DB::columnExists($this->getParam('db_table'), array(100 if (!$db->columnExists($this->getParam('db_table'), array( 93 101 'lock_id', 94 102 'record_table', … … 99 107 'lock_datetime', 100 108 ), 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__);109 $app->logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), LOG_ALERT, __FILE__, __LINE__); 102 110 trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), E_USER_ERROR); 103 111 } … … 120 128 121 129 /** 122 * Return the value of a param setting.123 * 124 * @access 125 * @param string $params Which paramto return.126 * @return mixed Configured paramvalue.130 * Return the value of a parameter, if it exists. 131 * 132 * @access public 133 * @param string $param Which parameter to return. 134 * @return mixed Configured parameter value. 127 135 */ 128 136 function getParam($param) 129 137 { 138 $app =& App::getInstance(); 139 130 140 if (isset($this->_params[$param])) { 131 141 return $this->_params[$param]; 132 142 } else { 133 App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);143 $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_NOTICE, __FILE__, __LINE__); 134 144 return null; 135 145 } … … 147 157 function select($record_table_or_lock_id, $record_key=null, $record_val=null) 148 158 { 159 $app =& App::getInstance(); 160 $db =& DB::getInstance(); 161 149 162 $this->initDB(); 150 163 … … 154 167 if (is_numeric($record_table_or_lock_id) && !isset($record_key) && !isset($record_val)) { 155 168 // Get lock data by lock_id. 156 $qid = DB::query("169 $qid = $db->query(" 157 170 SELECT * FROM " . $this->getParam('db_table') . " 158 WHERE lock_id = '" . DB::escapeString($record_table_or_lock_id) . "'171 WHERE lock_id = '" . $db->escapeString($record_table_or_lock_id) . "' 159 172 "); 160 173 } else { 161 174 // Get lock data by record specs 162 $qid = DB::query("175 $qid = $db->query(" 163 176 SELECT * FROM " . $this->getParam('db_table') . " 164 WHERE record_table = '" . DB::escapeString($record_table_or_lock_id) . "'165 AND record_key = '" . DB::escapeString($record_key) . "'166 AND record_val = '" . DB::escapeString($record_val) . "'177 WHERE record_table = '" . $db->escapeString($record_table_or_lock_id) . "' 178 AND record_key = '" . $db->escapeString($record_key) . "' 179 AND record_val = '" . $db->escapeString($record_val) . "' 167 180 "); 168 181 } 169 182 if ($this->data = mysql_fetch_assoc($qid)) { 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__);183 $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 184 /// FIX ME: What if admin set lock, but public user is current lock user? 172 185 $this->data['editor'] = $this->_auth->getUsername($this->data['set_by_admin_id']); 173 186 return true; 174 187 } else { 175 App::logMsg(sprintf('No locked record: %s %s %s', $record_table_or_lock_id, $record_key, $record_val), LOG_DEBUG, __FILE__, __LINE__);188 $app->logMsg(sprintf('No locked record: %s %s %s', $record_table_or_lock_id, $record_key, $record_val), LOG_DEBUG, __FILE__, __LINE__); 176 189 return false; 177 190 } … … 196 209 function isMine() 197 210 { 211 $db =& DB::getInstance(); 212 198 213 $this->initDB(); 199 214 200 215 if (isset($this->data['lock_id'])) { 201 $qid = DB::query("SELECT * FROM " . $this->getParam('db_table') . " WHERE lock_id = '" . DB::escapeString($this->data['lock_id']) . "'");216 $qid = $db->query("SELECT * FROM " . $this->getParam('db_table') . " WHERE lock_id = '" . $db->escapeString($this->data['lock_id']) . "'"); 202 217 if ($lock = mysql_fetch_assoc($qid)) { 203 218 return ($lock['set_by_admin_id'] == $this->_auth->getVal('user_id')); … … 222 237 function set($record_table, $record_key, $record_val, $title='') 223 238 { 239 $db =& DB::getInstance(); 240 224 241 $this->initDB(); 225 242 … … 228 245 229 246 // Remove previous locks if exist. Is this better than using a REPLACE INTO? 230 DB::query("247 $db->query(" 231 248 DELETE FROM " . $this->getParam('db_table') . " 232 WHERE record_table = '" . DB::escapeString($record_table) . "'233 AND record_key = '" . DB::escapeString($record_key) . "'234 AND record_val = '" . DB::escapeString($record_val) . "'249 WHERE record_table = '" . $db->escapeString($record_table) . "' 250 AND record_key = '" . $db->escapeString($record_key) . "' 251 AND record_val = '" . $db->escapeString($record_val) . "' 235 252 "); 236 253 237 254 // Set new lock. 238 DB::query("255 $db->query(" 239 256 INSERT INTO " . $this->getParam('db_table') . " ( 240 257 record_table, … … 245 262 lock_datetime 246 263 ) VALUES ( 247 '" . DB::escapeString($record_table) . "',248 '" . DB::escapeString($record_key) . "',249 '" . DB::escapeString($record_val) . "',250 '" . DB::escapeString($title) . "',251 '" . DB::escapeString($this->_auth->getVal('user_id')) . "',264 '" . $db->escapeString($record_table) . "', 265 '" . $db->escapeString($record_key) . "', 266 '" . $db->escapeString($record_val) . "', 267 '" . $db->escapeString($title) . "', 268 '" . $db->escapeString($this->_auth->getVal('user_id')) . "', 252 269 NOW() 253 270 ) 254 271 "); 255 $lock_id = mysql_insert_id( DB::getDBH());272 $lock_id = mysql_insert_id($db->getDBH()); 256 273 257 274 // Must register this locked record as the current. … … 266 283 function remove() 267 284 { 285 $app =& App::getInstance(); 286 $db =& DB::getInstance(); 287 268 288 $this->initDB(); 269 289 … … 272 292 273 293 // Delete a specific lock. 274 DB::query("294 $db->query(" 275 295 DELETE FROM " . $this->getParam('db_table') . " 276 WHERE lock_id = '" . DB::escapeString($this->data['lock_id']) . "'296 WHERE lock_id = '" . $db->escapeString($this->data['lock_id']) . "' 277 297 "); 278 298 279 App::logMsg(sprintf('Removing lock: %s', $this->data['lock_id']), LOG_DEBUG, __FILE__, __LINE__);299 $app->logMsg(sprintf('Removing lock: %s', $this->data['lock_id']), LOG_DEBUG, __FILE__, __LINE__); 280 300 } 281 301 … … 285 305 function removeAll($user_id=null) 286 306 { 307 $app =& App::getInstance(); 308 $db =& DB::getInstance(); 309 287 310 $this->initDB(); 288 311 … … 292 315 if (isset($user_id)) { 293 316 // Delete specific user's locks. 294 DB::query("DELETE FROM " . $this->getParam('db_table') . " WHERE set_by_admin_id = '" . DB::escapeString($user_id) . "'");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__);317 $db->query("DELETE FROM " . $this->getParam('db_table') . " WHERE set_by_admin_id = '" . $db->escapeString($user_id) . "'"); 318 $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__); 296 319 } else { 297 320 // 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__);321 $db->query("DELETE FROM " . $this->getParam('db_table') . ""); 322 $app->logMsg(sprintf('All record locks deleted by %s %s', $this->_auth->getVal('auth_name'), $this->_auth->getVal('username')), LOG_DEBUG, __FILE__, __LINE__); 300 323 } 301 324 } … … 306 329 function _auto_timeout() 307 330 { 331 $db =& DB::getInstance(); 332 308 333 static $_timeout_run = false; 309 334 … … 312 337 if (!$_timeout_run) { 313 338 // Delete all old locks. 314 DB::query("339 $db->query(" 315 340 DELETE FROM " . $this->getParam('db_table') . " 316 341 WHERE DATE_ADD(lock_datetime, INTERVAL '" . $this->getParam('auto_timeout') . "' SECOND) < NOW() … … 325 350 function dieErrorPage() 326 351 { 327 App::dieURL(sprintf('%s?lock_id=%s&boomerang=%s', $this->getParam('error_url'), $this->data['lock_id'], urlencode(absoluteMe()))); 352 $app =& App::getInstance(); 353 354 $app->dieURL(sprintf('%s?lock_id=%s&boomerang=%s', $this->getParam('error_url'), $this->data['lock_id'], urlencode(absoluteMe()))); 328 355 } 329 356 … … 333 360 function printErrorHTML() 334 361 { 362 $app =& App::getInstance(); 363 335 364 ?> 336 365 <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 337 <?php App::printHiddenSession() ?>366 <?php $app->printHiddenSession() ?> 338 367 <input type="hidden" name="lock_id" value="<?php echo $this->getID(); ?>" /> 339 368
Note: See TracChangeset
for help on using the changeset viewer.