Bug #24790 ยป 17289.diff

Administrator Admin, 2011-01-25 10:39

View differences:

t3lib/formprotection/class.t3lib_formprotection_abstract.php (working copy)
60 60
	 * checking.
61 61
	 */
62 62
	public function __construct() {
63
		$this->retrieveTokens();
63
		$this->tokens = $this->retrieveTokens();
64 64
	}
65 65

  
66 66
	/**
t3lib/formprotection/class.t3lib_formprotection_backendformprotection.php (working copy)
169 169
			$tokens = array();
170 170
		}
171 171

  
172
		$this->tokens = $tokens;
172
		return $tokens;
173
	}
174

  
175
	/**
176
	 * It might be that two (or more) scripts are executed at the same time,
177
	 * which would lead to a race condition, where both (all) scripts retrieve
178
	 * the same tokens from the session, so the script that is executed
179
	 * last will overwrite the tokens generated in the first scripts.
180
	 * So before writing all tokens back to the session we need to get the
181
	 * current tokens from the session again.
182
	 *
183
	 */
184
	protected function updateTokens() {
185
		$this->backendUser->user = $this->backendUser->fetchUserSession(TRUE);
186
		$tokens = $this->retrieveTokens();
187
		$this->tokens = array_merge($this->tokens, $tokens);
173 188
	}
174 189

  
175 190
	/**
......
179 194
	 * @return void
180 195
	 */
181 196
	public function persistTokens() {
197
		$lockObject = $this->acquireLock();
198

  
199
		$this->updateTokens();
182 200
		$this->backendUser->setAndSaveSessionData('formTokens', $this->tokens);
201

  
202
		$this->releaseLock($lockObject);
203
	}
204

  
205
	/**
206
	 * Tries to acquire a lock to not allow a race condition.
207
	 *
208
	 * @return t3lib_lock|FALSE The lock object or FALSE
209
	 */
210
	protected function acquireLock() {
211
		$identifier = 'persistTokens' . $this->backendUser->id;
212
		try {
213
			$lockObject = t3lib_div::makeInstance('t3lib_lock', $identifier, 'simple');
214
			$lockObject->setEnableLogging(FALSE);
215
			$success = $lockObject->acquire();
216
		} catch (Exception $e) {
217
			t3lib_div::sysLog('Locking: Failed to acquire lock: '.$e->getMessage(), 't3lib_formprotection_BackendFormProtection', t3lib_div::SYSLOG_SEVERITY_ERROR);
218
			$success = false;	// If locking fails, return with false and continue without locking
219
		}
220

  
221
		return $success ? $lockObject : FALSE;
222
	}
223

  
224
	/**
225
	 * Releases the lock if it was acquired before.
226
	 *
227
	 * @return boolean
228
	 */
229
	protected function releaseLock(&$lockObject) {
230
		$success = false;
231
			// If lock object is set and was acquired, release it:
232
		if (is_object($lockObject) && $lockObject instanceof t3lib_lock && $lockObject->getLockStatus()) {
233
			$success = $lockObject->release();
234
			$lockObject = null;
235
		}
236

  
237
		return $success;
183 238
	}
184 239
}
185 240

  
t3lib/class.t3lib_lock.php (working copy)
54 54

  
55 55
	protected $loops = 150; // Number of times a locked resource is tried to be acquired. This is only used by manual locks like the "simple" method.
56 56
	protected $step = 200; // Milliseconds after lock acquire is retried. $loops * $step results in the maximum delay of a lock. Only used by manual locks like the "simple" method.
57

  
57
	protected $syslogFacility = 'cms';
58
	protected $isLoggingEnabled = TRUE;
58 59

  
59 60
	/**
60 61
	 * Constructor:
......
269 270
	}
270 271

  
271 272
	/**
273
	 * Sets the facility (extension name) for the syslog entry.
274
	 *
275
	 * @param string $syslogFacility
276
	 */
277
	public function setSyslogFacility($syslogFacility) {
278
		$this->syslogFacility = $syslogFacility;
279
	}
280

  
281
	/**
282
	 * Enable/ disable logging
283
	 *
284
	 * @param boolean $isLoggingEnabled
285
	 */
286
	public function setEnableLogging($isLoggingEnabled) {
287
		$this->isLoggingEnabled = $isLoggingEnabled;
288
	}
289

  
290
	/**
272 291
	 * Adds a common log entry for this locking API using t3lib_div::sysLog().
273 292
	 * Example: 25-02-08 17:58 - cms: Locking [simple::0aeafd2a67a6bb8b9543fb9ea25ecbe2]: Acquired
274 293
	 *
......
277 296
	 * @return	void
278 297
	 */
279 298
	public function sysLog($message, $severity = 0) {
280
		t3lib_div::sysLog('Locking [' . $this->method . '::' . $this->id . ']: ' . trim($message), 'cms', $severity);
299
		if ($this->isLoggingEnabled) {
300
			t3lib_div::sysLog('Locking [' . $this->method . '::' . $this->id . ']: ' . trim($message), $this->syslogFacility, $severity);
301
		}
281 302
	}
282 303
}
283 304

  
    (1-1/1)