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:
<?php
class setupMutex {
private $locked = NULL;
private $ignoreUseAbort = NULL;
private $mutex = NULL;
private $lock = NULL;
private $lockfolder;
function __construct() {
if (function_exists('flock')) {
$this->lockfolder = dirname(dirname(dirname(__FILE__))) . '/' . DATA_FOLDER . '/' . MUTEX_FOLDER;
if (!file_exists($this->lockfolder))
setup::mkdir_r($this->lockfolder, 0777);
if (file_exists($this->lockfolder))
$this->lock = 'sP';
}
return $this->lock;
}
function __destruct() {
if ($this->locked) {
$this->unlock();
}
}
public function lock() {
if (!$this->locked && $this->lock) {
if ($this->mutex = @fopen($this->lockfolder . '/' . $this->lock, 'wb')) {
if (flock($this->mutex, LOCK_EX)) {
$this->locked = true;
$this->ignoreUserAbort = ignore_user_abort(true);
}
}
}
return $this->locked;
}
public function unlock() {
if ($this->locked) {
$this->locked = false;
ignore_user_abort($this->ignoreUserAbort);
flock($this->mutex, LOCK_UN);
fclose($this->mutex);
return true;
}
return false;
}
}