<?
include("config/connect.php");
include("functions.php");
//ini_set("max_execution_time",0);	
set_time_limit(0);

    if($_REQUEST["t"]!=""){
    	$t=$_REQUEST["t"];
  	}else{
  		$t='60';
  	}
		$qrygens = "select * from general_setting where id='1'";
		$resgens = mysql_query($qrygens);
		$objgens = mysql_fetch_object($resgens);
		$minimumbidprice = $objgens->min_bid_price;

		$qry = "select * from auction_pause_management where id='1'";
		$rs = mysql_query($qry);
		$total = mysql_num_rows($rs);
		$ob = mysql_fetch_object($rs);
		$st = explode(":",$ob->pause_start_time);
		$et = explode(":",$ob->pause_end_time);
		
		$querysel = "select auctionId from auction where auc_status='2' and pause_status='1' limit 0,1";
		$resultsel = mysql_query($querysel) or die(mysql_error());
		$totalauctpassel = mysql_num_rows($resultsel);
		
		
		$newpauseglobalcounter = 1;
		$pausestatuscounter = 1;
		$ampmset = 1;
		$auction_pause_status = 0;

		if($totalauctpassel>0)
		{
			$_SESSION["starttime"] = $ob->pause_start_timestamp;
			$auction_pause_status = 1;
			//fetch from query which is set when auction is actually paused.
		}
		else
		{
			$_SESSION["starttime"] = mktime($st[0],$st[1],$st[2],date("m"),date("d"),date("Y"));
			
		}				

		if(CheckTimeGreater($st[0],$st[1],$st[2],$et[0],$et[1],$et[2])==1)
		{			
			if($totalauctpassel>0)
			{
				if(date("Y-m-d")>date("Y-m-d",$_SESSION["starttime"]))
				{
					$_SESSION["endtime"] = mktime($et[0],$et[1],$et[2],date("m"),date("d"),date("Y"));
				}
				else
				{
					$_SESSION["endtime"] = mktime($et[0],$et[1],$et[2],date("m"),date("d")+1,date("Y"));
				}
				// if server date is greater then timestamp date then dont add one day otherwise add one day in date.
			}
			else
			{
				$_SESSION["endtime"] = mktime($et[0],$et[1],$et[2],date("m"),date("d")+1,date("Y"));
			}
		}
		else
		{			
			$_SESSION["endtime"] = mktime($et[0],$et[1],$et[2],date("m"),date("d"),date("Y"));
			
		}		
		
		$counter = 0;
		$auctiontime = 0;

		$scriptstarttime = mktime(date("H"),date("i"),date("s"),date("m"),date("d"),date("Y"));		
		
		if($scriptstarttime>mktime(12,0,0,date("m"),date("d"),date("Y")))
		{
			$scriptendtime = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
		}
		else
		{
			$scriptendtime = mktime(12,0,0,date("m"),date("d"),date("Y"));
		}		
		//echo $scriptstarttime."<br>";
		//echo $scriptendtime."<br>";
		
		$qryupdtime2 = "update auction_pause_management set referral_bids='1' where id='4'"; 		
		
		mysql_query($qryupdtime2);
		
		while($auction==FALSE)
		{
			$loopstarttime = microtime(true);
			if(time()>=$scriptstarttime && time()<=$scriptendtime)
			{
				$qryupdtime = "update auction_pause_management set referral_bids=referral_bids+1 where id='4'";
				mysql_query($qryupdtime);
			
				$qrysel = "select * from auc_due_table adt left join auction a on a.auctionID=adt.auction_id where auc_due_time!=0 and a.pause_status=0 order by auction_id";
				$ressel = mysql_query($qrysel);
				$total = mysql_num_rows($ressel);
				while($obj = mysql_fetch_array($ressel))
				{
					$oldtime = $obj["auc_due_time"];
					if($oldtime==0)
					{
						$newtime = 0;
					}
					else
					{
						$newtime = $oldtime-1;
					}
					$aid = $obj["auction_id"];
					$qryupd = "update auc_due_table set auc_due_time=$newtime where auction_id='$aid'";
					mysql_query($qryupd) or die(mysql_error());
					

					$qrygens2 = "select sysbid_flag from general_setting where id='1'";
					$resgens2 = mysql_query($qrygens2);
					$objgens2 = mysql_fetch_object($resgens2);
					$sysbid_flag = $objgens2->sysbid_flag;
					
					if($sysbid_flag=='1')
					{
						if($newtime<=10 && $obj["auction_min_price"]!="0.00")
						{
							if($newtime==rand(1,$newtime))
							{
								$auctiondueprice = GetAuctionMinPrice($obj["auctionID"],$minimumbidprice,$obj["fixedpriceauction"],$obj["offauction"],$obj["auc_fixed_price"],$obj["auc_due_price"]);
		
								if($auctiondueprice<$obj["auction_min_price"])
								{
									PlaceSystemBid($obj["auctionID"]);
								}
							}
						}
					}
	
					if($newtime==0)
					{
						UpdateAuction($aid);				
					}
					AddBidButler($aid,$newtime);
				}
				FutureAuctionManage();
	
				if(time()>=$_SESSION["starttime"] && $_SESSION["endtime"]>=time() && $auction_pause_status==0)
				{
					PauseDayAuction();
					$qryupdtstamp = "update auction_pause_management set pause_start_timestamp='".$_SESSION["starttime"]."' where id='1'";
					mysql_query($qryupdtstamp) or die(mysql_error());
					Makeentryinlog($_SESSION["starttime"],$_SESSION["endtime"],time(),$newpauseglobalcounter);
					$auction_pause_status = 1;
					$newpauseglobalcounter++;
				}
				if(time()>$_SESSION["endtime"] && $auction_pause_status==1)
				{
					RunDayAuction();
					Makeentryinlog($_SESSION["starttime"],$_SESSION["endtime"],time(),$newpauseglobalcounter);
					$auction_pause_status = 2;
					$newpauseglobalcounter++;
				}
				$counter++;
				if($counter>=600)
				{
					$qrygens = "select * from general_setting where id='1'";
					$resgens = mysql_query($qrygens);
					$objgens = mysql_fetch_object($resgens);
					$minimumbidprice = $objgens->min_bid_price;

					$qry = "select * from auction_pause_management where id='1'";
					$rs = mysql_query($qry);
					$total = mysql_num_rows($rs);
					$ob = mysql_fetch_object($rs);
					$st = explode(":",$ob->pause_start_time);
					$et = explode(":",$ob->pause_end_time);

					$querysel = "select auctionId from auction where auc_status='2' and pause_status='1' limit 0,1";
					$resultsel = mysql_query($querysel) or die(mysql_error());
					$totalauctpassel = mysql_num_rows($resultsel);
				
					if($totalauctpassel>0)
					{
						$_SESSION["starttime"] = $ob->pause_start_timestamp;
						$auction_pause_status = 1;
						//fetch from query which is set when auction is actually paused.
					}
					else
					{
						$_SESSION["starttime"] = mktime($st[0],$st[1],$st[2],date("m"),date("d"),date("Y"));
						$auction_pause_status = 0;
						$pausestatuscounter++;
					}
							
					if(CheckTimeGreater($st[0],$st[1],$st[2],$et[0],$et[1],$et[2])==1)
					{
						if($totalauctpassel>0)
						{
							if(date("Y-m-d")>date("Y-m-d",$_SESSION["starttime"]))
							{
								$_SESSION["endtime"] = mktime($et[0],$et[1],$et[2],date("m"),date("d"),date("Y"));
							}
							else
							{
								$_SESSION["endtime"] = mktime($et[0],$et[1],$et[2],date("m"),date("d")+1,date("Y"));
							}
							// if server date is greater then timestamp date then dont add one day otherwise add one day in date.
						}
						else
						{
							$_SESSION["endtime"] = mktime($et[0],$et[1],$et[2],date("m"),date("d")+1,date("Y"));
						}
					}
					else
					{
						$_SESSION["endtime"] = mktime($et[0],$et[1],$et[2],date("m"),date("d"),date("Y"));
					}
					Makeentryinlog($_SESSION["starttime"],$_SESSION["endtime"],time(),$newpauseglobalcounter);							
					if($ob->auction_end==1)
					{
						echo "exit";
						exit;
					}
					$counter = 0;
				}
				
				//mysql_query("delete from bidsinfo");
				//$qrygens = "call CastBidsInfo()";
				//$resgens = mysql_query($qrygens);
				
				//mysql_query("delete from bidsinfo");
				//CastBidsInfo();
				
				
				$loopendtime = microtime(true);
				$consumingtime = (1000000 - (($loopendtime - $loopstarttime) * 1000000));
				if($consumingtime>0)
				{
					if(phpinfo_array()=='Windows_NT'){
						msleep($consumingtime); //windows
					}
					if(phpinfo_array()!='Windows_NT'){
						usleep($consumingtime);//unix
					}										
				}
				
				$auctiontime++;
				//echo $auctiontime."<br>";
				if($auctiontime>=$t)
				{
					$auction = TRUE;
					echo "Success";			
				}
			}
			else
			{
					$auction = TRUE;
					echo "Success1";			
					//header('Location: update_records.php');
			}	
		}		
		
// Helper function
function getmicrotime(){
    list($usec, $sec) = explode(' ',microtime());
    return ((float)$usec + (float)$sec);
}

// usleep alias function for windows
function msleep($micro_seconds=0) {
    $stop  = getmicrotime() + ($micro_seconds / 1000000);
    while (getmicrotime() <= $stop) {
        // loop
    }
    return true;
} 

function phpinfo_array($return=false){
 /* Andale!  Andale!  Yee-Hah! */
 ob_start(); 
 phpinfo(-1);
 
 $pi = preg_replace(
 array('#^.*<body>(.*)</body>.*$#ms', '#<h2>PHP License</h2>.*$#ms',
 '#<h1>Configuration</h1>#',  "#\r?\n#", "#</(h1|h2|h3|tr)>#", '# +<#',
 "#[ \t]+#", '#&nbsp;#', '#  +#', '# class=".*?"#', '%&#039;%',
  '#<tr>(?:.*?)" src="(?:.*?)=(.*?)" alt="PHP Logo" /></a>'
  .'<h1>PHP Version (.*?)</h1>(?:\n+?)</td></tr>#',
  '#<h1><a href="(?:.*?)\?=(.*?)">PHP Credits</a></h1>#',
  '#<tr>(?:.*?)" src="(?:.*?)=(.*?)"(?:.*?)Zend Engine (.*?),(?:.*?)</tr>#',
  "# +#", '#<tr>#', '#</tr>#'),
 array('$1', '', '', '', '</$1>' . "\n", '<', ' ', ' ', ' ', '', ' ',
  '<h2>PHP Configuration</h2>'."\n".'<tr><td>PHP Version</td><td>$2</td></tr>'.
  "\n".'<tr><td>PHP Egg</td><td>$1</td></tr>',
  '<tr><td>PHP Credits Egg</td><td>$1</td></tr>',
  '<tr><td>Zend Engine</td><td>$2</td></tr>' . "\n" .
  '<tr><td>Zend Egg</td><td>$1</td></tr>', ' ', '%S%', '%E%'),
 ob_get_clean());

 $sections = explode('<h2>', strip_tags($pi, '<h2><th><td>'));
 unset($sections[0]);

 $pi = array();
 foreach($sections as $section){
   $n = substr($section, 0, strpos($section, '</h2>'));
   preg_match_all(
   '#%S%(?:<td>(.*?)</td>)?(?:<td>(.*?)</td>)?(?:<td>(.*?)</td>)?%E%#',
     $section, $askapache, PREG_SET_ORDER);
   foreach($askapache as $m)
       $pi[$n][$m[1]]=(!isset($m[3])||$m[2]==$m[3])?$m[2]:array_slice($m,2);
 }
 //echo $pi['Environment']['OS'];
 //return ($return === false) ? print_r($pi) : $pi;
 
 return ($pi['Environment']['OS']!='')?$pi['Environment']['OS'] : $pi['Environment']['OSTYPE'];
 
}


function CastBidsInfo()
{
	$sql = "select auctionID from auction where auc_status='2'";
	$result = mysql_query($sql);
	$total = mysql_num_rows($result);
  if($total>0)
  {	
		while($obj=mysql_fetch_array($result))
	 	{
	 		$s_auction_id = $obj['auctionID'];
	 		$sql2 = "select * from bid_account where read_flag=0 and auction_id='$s_auction_id' and bidding_type='s'";
			$result2 = mysql_query($sql2);
			$total2 = mysql_num_rows($result2);
			if($total2>0){
				$sql3 = "
				select pause_status,auc_status,auc_due_time,auc_due_price,auc_plus_price,auc_plus_time,pennyauction          
         from auc_due_table adt 
         left join auction a on adt.auction_id=a.auctionID 
         left join auction_management am on a.time_duration=am.auc_manage 
        where auction_id='$s_auction_id' ";
				$result3 = mysql_query($sql3);
        $obj3=mysql_fetch_array($result3);
        $s_pause_status = $obj3['pause_status'];
        $s_auc_status = $obj3['auc_status'];
        $s_count = $total2;
        if($s_pause_status!='1' && $s_auc_status=='2')
        {					
					$plusprice = $obj3['auc_plus_price'];
					$plustime = $obj3['auc_plus_time'];
		      
					if($s_pennyauction=='1')
					{
           	 $totalprice = $s_count * 0.01;
        	}else{
           	 $totalprice = $s_count * $plusprice;
          }
        	$totaltime = $s_count * $plustime;
                	
        	$sql4 = "insert into bidsinfo(auctionID,bids_info) values('$s_auction_id',concat('$s_count','´Î³ö¼Û +£¤','$totalprice',' +','$totaltime','Ãë'))";
        	$sql5 = "update bid_account set read_flag=1 where read_flag=0 and auction_id='$s_auction_id' and bidding_type='s'";   
    			mysql_query($sql4) ;
    			mysql_query($sql5) ;
        }
			}
				 		
		}
	}
		
} /*end function*/

?>
