<?php 

if ( ! function_exists('createLockFile'))
{
	function createLockFile ($lockFilePrefix, $maxReach, $timeToUnlock, $lockFilePath = '', $checkLockOnly = FALSE, $colSep = '|')
	{
		$currentTimeStamp = time();
		$filePath = $lockFilePath.$lockFilePrefix.'_lock';
		$fileExisted = file_exists($filePath);
		$content = '';
		$lastTimeLock = '';
		$lockCount = 0;
		$isLocked = 0;
		$ready = 0;
	
		if ($fileExisted) {
			$content = file_get_contents($filePath);
		}
	
		if ($content != '') {
			list($lockCount, $lastTimeLock, $isLocked) = explode ($colSep, $content);
		}
	
		$timeDiffMin = $lastTimeLock != '' ? dateDifference(date('Y-m-d H:i:s',$currentTimeStamp), date('Y-m-d H:i:s',$lastTimeLock), '%i') : 0;

		if ($lockCount < $maxReach && $timeDiffMin < $timeToUnlock && !$checkLockOnly) {
			$lockCount ++;
			$isLocked = $lockCount < $maxReach ? 0:1;
			$ready = 1;
		}
		else if ($timeDiffMin > $timeToUnlock) {
			$lockCount = 1;
			$isLocked = 0;
			$ready = 1;
		}
	
		$lockTime = ($isLocked == 1) ? $lastTimeLock : $currentTimeStamp;
		$content = $lockCount . $colSep . $lockTime . $colSep . $isLocked;
		
		if($ready == 1 && !$checkLockOnly) {
			$fh = fopen ($filePath,'w');
			fwrite ($fh, $content);
			fclose ($fh);
		}
	
		return (int)$isLocked;
	}
}

if ( ! function_exists('releaseLockFile'))
{
	function releaseLockFile ($lockFilePrefix, $lockFilePath = '')
	{
		$filePath = $lockFilePath.$lockFilePrefix.'_lock';
		$fileExisted = file_exists($filePath);
		if ($fileExisted) {
			unlink($filePath);
		}
	}
}

/**
 * dateDifference
 * Return difference between two dates
 *
 * @access	public
 * @param	date	date_1
 * @param	date	date_2
 * @param	string	differenceFormat ()
 * @return	string
 *
 * @example '%y Year %m Month %d Day %h Hours %i Minute %s Seconds', %y Year %m Month %d Day','%m Month %d Day'
 * @example '%d Day %h Hours', '%d Day', '%h Hours %i Minute %s Seconds', '%i Minute %s Seconds','%h Hours', '%a Days'
 */

if ( ! function_exists('dateDifference'))
{
	function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' )
	{
		$datetime1 = date_create($date_1);
		$datetime2 = date_create($date_2);

		$diff = date_diff($datetime1, $datetime2);
		$total = 0;
		switch( $differenceFormat){
			case "%y":
				$total = $diff->y + $diff->m / 12 + $diff->d / 365.25; break;
			case "%m":
				$total= $diff->y * 12 + $diff->m + $diff->d/30 + $diff->h / 24;
				break;
			case "%a":
				$total = $diff->y * 365.25 + $diff->m * 30 + $diff->d + $diff->h/24 + $diff->i / 60;
				break;
			case "%h":
				$total = ($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h + $diff->i/60;
				break;
			case "%i":
				$total = (($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h) * 60 + $diff->i + $diff->s/60;
				break;
			case "%s":
				$total = ((($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h) * 60 + $diff->i)*60 + $diff->s;
				break;
		}
		
		return (int)$total;
		/* if( $diff->invert)
			return -1 * $total;
		else    return $total; */
	}
}
