[619] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | if ( ! function_exists('createLockFile'))
|
---|
| 4 | {
|
---|
| 5 | function createLockFile ($lockFilePrefix, $maxReach, $timeToUnlock, $colSep = '|', $lockFilePath = '')
|
---|
| 6 | {
|
---|
| 7 | $currentTimeStamp = time();
|
---|
| 8 | $filePath = $lockFilePath.$lockFilePrefix.'_lock';
|
---|
| 9 | $fileExisted = file_exists($filePath);
|
---|
| 10 | $content = '';
|
---|
| 11 | $lastTimeLock = '';
|
---|
| 12 | $lockCount = 0;
|
---|
| 13 | $isLocked = 0;
|
---|
| 14 | $ready = 0;
|
---|
| 15 |
|
---|
| 16 | if ($fileExisted) {
|
---|
| 17 | $content = file_get_contents($filePath);
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | if ($content != '') {
|
---|
| 21 | list($lockCount, $lastTimeLock, $isLocked) = explode ($colSep, $content);
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | $timeDiffMin = $lastTimeLock != '' ? dateDifference(date('Y-m-d H:i:s',$currentTimeStamp), date('Y-m-d H:i:s',$lastTimeLock), '%i') : 0;
|
---|
| 25 |
|
---|
| 26 | if ($lockCount < $maxReach && $timeDiffMin < $timeToUnlock) {
|
---|
| 27 | $lockCount ++;
|
---|
| 28 | $isLocked = $lockCount < $maxReach ? 0:1;
|
---|
| 29 | $ready = 1;
|
---|
| 30 | }
|
---|
| 31 | else if ($timeDiffMin > $timeToUnlock) {
|
---|
| 32 | $lockCount = 0;
|
---|
| 33 | $isLocked = 0;
|
---|
| 34 | $ready = 1;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | $lockTime = ($isLocked == 1) ? $lastTimeLock : $currentTimeStamp;
|
---|
| 38 | $content = $lockCount . $colSep . $lockTime . $colSep . $isLocked;
|
---|
| 39 | if($ready == 1) {
|
---|
| 40 | $fh = fopen ($filePath,'w');
|
---|
| 41 | fwrite ($fh, $content);
|
---|
| 42 | fclose ($fh);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | return $lockCount;
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | if ( ! function_exists('releaseLockFile'))
|
---|
| 50 | {
|
---|
| 51 | function releaseLockFile ($lockFilePrefix, $lockFilePath = '')
|
---|
| 52 | {
|
---|
| 53 | $filePath = $lockFilePrefix.'_lock';
|
---|
| 54 | $fileExisted = file_exists($filePath);
|
---|
| 55 | if ($fileExisted) {
|
---|
| 56 | unlink($filePath);
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * dateDifference
|
---|
| 63 | * Return difference between two dates
|
---|
| 64 | *
|
---|
| 65 | * @access public
|
---|
| 66 | * @param date date_1
|
---|
| 67 | * @param date date_2
|
---|
| 68 | * @param string differenceFormat ()
|
---|
| 69 | * @return string
|
---|
| 70 | *
|
---|
| 71 | * @example '%y Year %m Month %d Day %h Hours %i Minute %s Seconds', %y Year %m Month %d Day','%m Month %d Day'
|
---|
| 72 | * @example '%d Day %h Hours', '%d Day', '%h Hours %i Minute %s Seconds', '%i Minute %s Seconds','%h Hours', '%a Days'
|
---|
| 73 | */
|
---|
| 74 |
|
---|
| 75 | if ( ! function_exists('dateDifference'))
|
---|
| 76 | {
|
---|
| 77 | function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' )
|
---|
| 78 | {
|
---|
| 79 | $datetime1 = date_create($date_1);
|
---|
| 80 | $datetime2 = date_create($date_2);
|
---|
| 81 |
|
---|
| 82 | $interval = date_diff($datetime1, $datetime2);
|
---|
| 83 |
|
---|
| 84 | return $interval->format($differenceFormat);
|
---|
| 85 |
|
---|
| 86 | }
|
---|
| 87 | }
|
---|