00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 require_once(PATH_t3lib.'class.t3lib_div.php');
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 class t3lib_lock {
00063 protected $method;
00064 protected $id;
00065 protected $resource;
00066 protected $filepointer;
00067 protected $isAcquired = false;
00068
00069 protected $loops = 150;
00070 protected $step = 200;
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 public function __construct($id, $method='', $loops=0, $steps=0) {
00087
00088
00089 $id = (string)$id;
00090 if (intval($loops)) {
00091 $this->loops = intval($loops);
00092 }
00093 if (intval($step)) {
00094 $this->step = intval($step);
00095 }
00096
00097
00098 if (in_array($method, array('disable', 'simple', 'flock', 'semaphore'))) {
00099 $this->method = $method;
00100 } else {
00101 throw new Exception('No such method "'.$method.'"');
00102 }
00103
00104 $success = false;
00105 switch ($this->method) {
00106 case 'simple':
00107 case 'flock':
00108 $path = PATH_site.'typo3temp/locks/';
00109 if (!is_dir($path)) {
00110 t3lib_div::mkdir($path);
00111 }
00112 $this->id = md5($id);
00113 $this->resource = $path.$this->id;
00114 $success = true;
00115 break;
00116 case 'semaphore':
00117 $this->id = abs(crc32($id));
00118 if (($this->resource = sem_get($this->id, 1))==true) {
00119 $success = true;
00120 }
00121 break;
00122 case 'disable':
00123 return false;
00124 break;
00125 }
00126
00127 return $success;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136 function __destruct() {
00137 $this->release();
00138 }
00139
00140
00141
00142
00143
00144
00145
00146
00147 public function acquire() {
00148 $noWait = true;
00149 $isAcquired = true;
00150
00151 switch ($this->method) {
00152 case 'simple':
00153 if (is_file($this->resource)) {
00154 $this->sysLog('Waiting for a different process to release the lock');
00155 $i = 0;
00156 while ($i<$this->loops) {
00157 $i++;
00158 usleep($this->step*1000);
00159 clearstatcache();
00160 if (!is_file($this->resource)) {
00161 $this->sysLog('Different process released the lock');
00162 $noWait = false;
00163 break;
00164 }
00165 }
00166 } else {
00167 $noWait = true;
00168 }
00169
00170 if (($this->filepointer = touch($this->resource)) == false) {
00171 throw new Exception('Lock file could not be created');
00172 }
00173 break;
00174 case 'flock':
00175 if (($this->filepointer = fopen($this->resource, 'w+')) == false) {
00176 throw new Exception('Lock file could not be opened');
00177 }
00178
00179 if (flock($this->filepointer, LOCK_EX|LOCK_NB) == true) {
00180 $noWait = true;
00181 } elseif (flock($this->filepointer, LOCK_EX) == true) {
00182 $noWait = false;
00183 } else {
00184 throw new Exception('Could not lock file "'.$this->resource.'"');
00185 }
00186 break;
00187 case 'semaphore':
00188 if (sem_acquire($this->resource)) {
00189
00190 $noWait = false;
00191 }
00192 break;
00193 case 'disable':
00194 $noWait = false;
00195 $isAcquired = false;
00196 break;
00197 }
00198
00199 $this->isAcquired = $isAcquired;
00200 return $noWait;
00201 }
00202
00203
00204
00205
00206
00207
00208 public function release() {
00209 if (!$this->isAcquired) {
00210 return true;
00211 }
00212
00213 $success = true;
00214 switch ($this->method) {
00215 case 'simple':
00216 if (unlink($this->resource) == false) {
00217 $success = false;
00218 }
00219 break;
00220 case 'flock':
00221 if (flock($this->filepointer, LOCK_UN) == false) {
00222 $success = false;
00223 }
00224 fclose($this->filepointer);
00225 unlink($this->resource);
00226 break;
00227 case 'semaphore':
00228 if (@sem_release($this->resource)) {
00229 sem_remove($this->resource);
00230 } else {
00231 $success = false;
00232 }
00233 break;
00234 case 'disable':
00235 $success = false;
00236 break;
00237 }
00238
00239 $this->isAcquired = false;
00240 return $success;
00241 }
00242
00243
00244
00245
00246
00247
00248 public function getMethod() {
00249 return $this->method;
00250 }
00251
00252
00253
00254
00255
00256
00257 public function getId() {
00258 return $this->id;
00259 }
00260
00261
00262
00263
00264
00265
00266
00267 public function getResource() {
00268 return $this->resource;
00269 }
00270
00271
00272
00273
00274
00275
00276 public function getLockStatus() {
00277 return $this->isAcquired;
00278 }
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 public function sysLog($message, $severity=0) {
00289 t3lib_div::sysLog('Locking ['.$this->method.'::'.$this->id.']: '.trim($message), 'cms', $severity);
00290 }
00291 }
00292
00293
00294 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_lock.php']) {
00295 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_lock.php']);
00296 }
00297 ?>