00001 <?php
00005 require_once('ILock.php');
00006
00035 class FileLock implements ILock
00036 {
00044 function FileLock($lockPath_)
00045 {
00046 $this->setLockPath($lockPath_);
00047 }
00048
00056 public function setLockPath($lockPath_)
00057 {
00058 $this->_lockPath = rtrim($lockPath_, '/') . '/';
00059
00060 if (!file_exists($this->_lockPath))
00061 {
00062 if (!@mkdir($this->_lockPath, 0755, true))
00063 {
00064
00065
00066
00067 echo('Couldnt create path');
00068 exit;
00069 }
00070 }
00071 }
00072
00080 public function getLockPath()
00081 {
00082
00083
00084 return $this->_lockPath;
00085 }
00086
00092 public function deleteAllLocks()
00093 {
00094 $path = $this->getLockPath();
00095
00096 if
00097 (
00098 !empty($path) &&
00099 $path != '/'
00100 )
00101 {
00102 exec('rm ' . $path . '*.lock', $arr, $retval);
00103 return true;
00104 }
00105 else
00106 {
00107 return false;
00108 }
00109 }
00110
00132 public function isLockExpired
00133 (
00134 $lockName_,
00135 $lockTime_ = 60,
00136 $compressName_ = true
00137 )
00138 {
00139 $fileName = $this->_getFileName($lockName_, $compressName_);
00140
00141 $retVal = false;
00142 if
00143 (
00144 !file_exists($fileName) ||
00145 filemtime($fileName) < (time() - $lockTime_)
00146 )
00147 {
00148 $handle = @fopen($fileName, 'w');
00149
00150 if ($handle !== false)
00151 {
00152 fwrite($handle, $lockName_);
00153 $retVal = true;
00154 fclose($handle);
00155 }
00156 }
00157
00158 return $retVal;
00159 }
00160
00164 private function _getFileName($lockName_, $compressName_)
00165 {
00166 if ($compressName_)
00167 {
00168 $lockName_ = md5($lockName_);
00169 }
00170
00171 $fileName = $this->getLockPath() . md5($lockName_) . '.lock';
00172
00173 return $fileName;
00174 }
00175
00180 private $_lockPath;
00181 }
00182
00183 ?>