<?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);
		//$diff = DateTime::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; */
	}
}

if(!function_exists('date_diff')) {
  class DateInterval {
    public $y;
    public $m;
    public $d;
    public $h;
    public $i;
    public $s;
    public $invert;
    public $days;
 
    public function format($format) {
      $format = str_replace('%R%y', 
        ($this->invert ? '-' : '+') . $this->y, $format);
      $format = str_replace('%R%m', 
         ($this->invert ? '-' : '+') . $this->m, $format);
      $format = str_replace('%R%d', 
         ($this->invert ? '-' : '+') . $this->d, $format);
      $format = str_replace('%R%h', 
         ($this->invert ? '-' : '+') . $this->h, $format);
      $format = str_replace('%R%i', 
         ($this->invert ? '-' : '+') . $this->i, $format);
      $format = str_replace('%R%s', 
         ($this->invert ? '-' : '+') . $this->s, $format);
 
      $format = str_replace('%y', $this->y, $format);
      $format = str_replace('%m', $this->m, $format);
      $format = str_replace('%d', $this->d, $format);
      $format = str_replace('%h', $this->h, $format);
      $format = str_replace('%i', $this->i, $format);
      $format = str_replace('%s', $this->s, $format);
 
      return $format;
    }
  }
 
  function date_diff(DateTime $date1, DateTime $date2) {
 
    $diff = new DateInterval();
 
    if($date1 > $date2) {
      $tmp = $date1;
      $date1 = $date2;
      $date2 = $tmp;
      $diff->invert = 1;
    } else {
      $diff->invert = 0;
    }
 
    $diff->y = ((int) $date2->format('Y')) - ((int) $date1->format('Y'));
    $diff->m = ((int) $date2->format('n')) - ((int) $date1->format('n'));
    if($diff->m < 0) {
      $diff->y -= 1;
      $diff->m = $diff->m + 12;
    }
    $diff->d = ((int) $date2->format('j')) - ((int) $date1->format('j'));
    if($diff->d < 0) {
      $diff->m -= 1;
      $diff->d = $diff->d + ((int) $date1->format('t'));
    }
    $diff->h = ((int) $date2->format('G')) - ((int) $date1->format('G'));
    if($diff->h < 0) {
      $diff->d -= 1;
      $diff->h = $diff->h + 24;
    }
    $diff->i = ((int) $date2->format('i')) - ((int) $date1->format('i'));
    if($diff->i < 0) {
      $diff->h -= 1;
      $diff->i = $diff->i + 60;
    }
    $diff->s = ((int) $date2->format('s')) - ((int) $date1->format('s'));
    if($diff->s < 0) {
      $diff->i -= 1;
      $diff->s = $diff->s + 60;
    }
 
    $start_ts   = $date1->format('U');
    $end_ts   = $date2->format('U');
    $days     = $end_ts - $start_ts;
    $diff->days  = round($days / 86400);
 
    if (($diff->h > 0 || $diff->i > 0 || $diff->s > 0))
      $diff->days += ((bool) $diff->invert)
        ? 1
        : -1;
 
    return $diff;
 
  }
 
}
