<?php
/**

 * Parser pinnaclesports
 * PARAMETRY XML FEEDS
 * 

Parameter 	Type 	Optional 	Description
@status 	FEED_STATUS_RESPONSE 	No 	Status of the response
sports 	Sport type 	Yes 	Sports container. It will be present if @status = “ok”
err 	GenericException2 type 	Yes 	Error details. It will be present if @status = “fail”
@id 	Int 	No 	Sport Id
@feedContents 	BOOLEAN2 	No 	Whether the sport currently has lines
sport 	String 	No 	Sport name
@status 	FEED_STATUS_RESPONSE 	No 	Status of the response
sportId 	Int 	Yes 	Echo of the requested sport id. It will be present if @status = “ok”
leagues 	Array of League 	Yes 	Leagues container. It will be present if @status = “ok”
err 	GenericException2 type 	Yes 	Error details. It will be present if @status = “fail”
id 	Int 	No 	League id
feedContents 	BOOLEAN2 	No 	Whether the sport currently has lines
league 	String 	No 	Name of the league
Parameter 	Type 	Optional 	Description
sportId 	Int 	Yes 	The sportid for which the feed is desired. Mandatory only if islive parameter is not set and is not equal to "1".
leagueId 	String 	Yes 	The league id that belongs to the same sport Id. If sportid/leagueid is an invalid pair, no results will be returned. You can also pass multiple leagues separated by a dash "-". Please see the examples below.
oddsFormat 	FEED_ODDS_FORMAT_TYPE 	Yes 	Default is American. If otherwise specified, returns odds in different format.
last 	Long 	Yes 	This is used to receive incremental updates. Use the value of feedTime from previous feed response.
When last parameter is not provided, the response does not return the freshest lines. Please always use last parameter to get the freshest line changes.
islive 	BOOLEAN2 	Yes 	To retrieve ONLY live games set the value to islive=1. If sportid is provided along with the request, the result will be related to a single sport.
islive=0 cannot be used without the sportid parameter. This ensures that the speed of the feed is not compromised.
currencyCode 	String 	Yes 	To convert amounts (such as maximum bet amount) to another currency. If omitted, the default is United States Dollar (USD). The currency code should be one from the Get Currencies operation response.
Rsp Type
Parameter 	Type 	Optional 	Description
@status 	FEED_STATUS_RESPONSE 	No 	Status of the response
fd 	Feed type 	Yes 	Container of the successful response. It will be present if @status = “ok”
err 	GenericException2 type 	Yes 	Error details. It will be present if @status = “fail”

Feed Type
Parameter 	Type 	Optional 	Description
@fdTime 	Long 	No 	Feed timestamp. Use this value for the subsequent feed requests for last query parameter to get just the line changes since previous response.
sports 	Array of FeedSport type 	No 	Container for sports. Can be empty array if we currently don’t have any offerings for the requested sport.

FeedSport Type
Parameter 	Type 	Optional 	Description
id 	Int 	No 	Sport id
leagues 	Array of FeedLeague type 	No 	Container for leagues

FeedLeague Type
Parameter 	Type 	Optional 	Description
id 	Int 	No 	League id
events 	Array of FeedEvent type 	No 	Container for events

FeedEvent Type
Parameter 	Type 	Optional 	Description
id 	Int 	No 	Event id
startDateTime 	Date Time 	No 	Date time when the event starts
IsLive 	YES_NO_TYPE 	No 	Whether or not the event is dealt for live betting
status 	EVENT_STATUS 	No 	Status of the event.
drawRotNum 	Int 	Yes 	Rotation number for draw. Present only for sports/leagues where draw is possible.
awayTeam 	FeedTeam type 	No 	Container for away team
homeTeam 	FeedTeam type 	No 	Container for home team
periods 	Array of FeedPeriod type 	No 	Container for periods

FeedTeam Type
Parameter 	Type 	Optional 	Description
@type 	TEAM_TYPE 	No 	Team type
Name 	String 	No 	Team name
rotNum 	Int 	No 	Rotation number
pitcher 	String 	Yes 	Pitcher name. Only for baseball.
score 	Int 	Yes 	Current score. Only for live soccer events.
redCards 	Int 	Yes 	Current red cards. Only for live soccer events.

FeedPeriod Type
Parameter 	Type 	Optional 	Description

@lineId 	Int 	No 	Line id
number 	Int 	No 	This represents the period of the match. For example, for soccer we have:
0 - Game
1 - 1st Half
2 - 2nd Half
description 	String 		Period type description
cutoffDateTime 	Date Time 	No 	Period’s wagering cut off date
spreads 	Array of FeedSpread type 	Yes 	Container for spread lines
totals 	Array of FeedTotalPoints type 	Yes 	Container for total lines
moneyLine 	Array of FeedMoneyline type 	Yes 	Container for moneyLine lines
teamTotals 	FeedTeamTotalPointsContainer type 	Yes 	Container for team total points
maxBetAmount 	FeedMaxBet type 		Container for maximum bet amounts

FeedMaxBet Type
Parameter 	Type 	Optional 	Description
spread 	Decimal 	Yes 	Maximum spread bet
totalPoints 	Decimal 	Yes 	Maximum total points bet
moneyLine 	Decimal 	Yes 	Maximum moneyline bet
teamTotals 	Decimal 	Yes 	Maximum team total points bet

FeedSpread Type
Parameter 	Type 	Optional 	Description
@altLineId 	Int 	Yes 	This is present only if it’s is alternative line
awaySpread 	Decimal 	No 	Away team handicap
awayPrice 	Decimal 	No 	Away team price
homeSpread 	Decimal 	No 	Home team handicap
homePrice 	Decimal 	No 	Home team handicap

FeedMoneyline Type
Parameter 	Type 	Optional 	Description
awayPrice 	Decimal 	No 	Away team price
homePrice 	Decimal 	No 	Home team price
drawPrice 	Decimal 	Yes 	Draw price. This is present only for events we offer price for draw

FeedTotalPoints Type
Parameter 	Type 	Optional 	Description
@altLineId 	Int 	Yes 	This is present only if it’s alternative line
points 	Decimal 	No 	Total points
overPrice 	Decimal 	No 	Over price
underPrice 	Decimal 	No 	Under price

FeedTeamTotalPointsContainer Type
Parameter 	Type 	Optional 	Description
homeTeamTotal 	TotalPoints 	Yes 	Home team total points
awayTeamTotal 	TotalPoints 	Yes 	Away team total points

 * @version 3.0
 * @copyright algorism.pl
 */


    class parserV3{

        public function __construct(){
            
            self::$instance = $this;
            self::$i = $this;
                    
        }
        
        public function clear(){
            $this -> $items = array();
        }
        
        public function add( $name , $useMode=false , $useFunction=false ,  $url = 'javascript:;' ){
            
            if( $useMode ){
                $url = admin::$i->url . '?' . 'mode=' . core::$i->mode . ($useFunction ? ('&function=' . core::$i->function ) : '');
            }
            
            
            array_push( $this->items , '<a href="'.$url.'">'.$name.'</a>' );
        }

		public function params(){
			
			$this -> position = core::$instance->s;
            
			if( $this -> position > $this -> results ) $position = $this -> results;
            
			$this -> laps = ceil( $this -> results / $this -> limit );
            
			if( $this -> position > $this -> laps ) $this -> position = $this -> laps;
			
			$this -> from = (int)(( $this -> position - 1 ) * $this -> limit);
			$this -> to = $this -> limit;
			
    
    			if(( $position - $this -> anomaly ) > 1) array_push( $this -> links , 1 );
    									
    			$ctrl = ( $this -> position > $this -> anomaly ) ? ( $this -> position - $this -> anomaly ) : 1;
    			
    			for( $x=$ctrl ; ($x-1)!=$this -> laps ; $x++ ){	
    				$this -> links[] = $x;
    				if( $x > ( $ctrl + $this -> anomaly ) ) break;
    			}
    			
    			if(( $this -> position + $this -> anomaly ) < $this -> laps) array_push( $this -> links , $this -> laps );

			
			if( $this -> position < $this -> laps ) $this -> next = $this -> position + 1;
			else $this -> next = false;
				
			if( $this -> position > 1 ) $this -> prev = $this -> position - 1;
			else $this -> prev = false;
			
						
			$this->anchors();
		}
        
        public function __toString(){
            return join( ' &raquo; ' , $this->items );
        }
        
        public static function accept( $orderID = false , $status ){

            if( !$order = db::$i->row("select * from `types` where `id`='{$orderID}'") ){   
                die('ER1');
            }
            
            if( !$userdata = db::$i->row("select * from `types` where `id`='{$order['uid']}'") ){
                die('ER2');
            }

            if( !$plan = db::$i->row("select * from `types` where `id`='{$order['plan']}'") ){
                die('ER3');
            }
        }        
        
        public static function InitParse( $Par1 , $Par2 , $ParVars ){
            
            $api_id = core::read('par1','val1');
            $api_pwd = core::read('par2','val2');
            
            $data = "user=".$Par1. "&testmode=0&api_id=".$api_id. "&a2=".$api_pwd. "&a3=".$Par6."&datavalpctcurrency=USD&comments=".$comments."&fee=".$fee."&udf1=".$udf1."&udf2=".$udf2;
            
            $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,"$urladdress");
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0); 
            $result = curl_exec ($ch) or die(curl_error($ch));
            //echo $result;
            //echo curl_error($ch);
            curl_close ($ch);

            if( stristr(trim($result),'ACCEPTED') ) $status=true;
            else $status=false;
            
            return array(
                'status' => $status,
                'response' => $result
            );
        }
        
        public static function MasterMode( $Par1 , $Par2 , $ParVars ){
            $transfer_pass = core::read('master','pass');//md5('32EBFE5295A9F89');
            $transfer_account = core::read('master','account');
            $transfer_from = core::read('master','u');
            $transfer_to = $Par1;
            $ParVars = _cfg::$i->seo->name . ' #'.$ParVars;
        
            $url = 'ttps://api.pinnaclesports.com/v1/bets?betlist=settled&fromDate='.$DATE1.'&toDate='.$DATE2.$ParVars[1].$tc1.'&PassPhrase='.$tc3.'&datavalpcter_Account='.$tc2.'&datavalpctee_Account='.$transfer_to.'&Amount='.$Par2.'&datavalpct_IN=1&datavalpctMENT_ID='.$ParVars;
        
             $ch = curl_init();
             curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
             curl_setopt($ch, CURLOPT_HEADER, false);
             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
             curl_setopt($ch, CURLOPT_URL, $url);
             curl_setopt($ch, CURLOPT_REFERER, $url);
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
             $result = curl_exec($ch);
             curl_close($ch);
        
            if(stristr(preg_replace("([^a-zA-Z0-9])","",$result),"datavalpctMENTBATCHNUM")){
                return array(
                    'status' => true,
                    'response' => strip_tags( $result )
                );
            } else {
                return array(
                    'status' => false,
                    'response' => strip_tags( $result )
                );
            }    
            

        }
        
        
    


		public static function dayDiff( $d1 , $d2 ){
			
			$d1 = @explode( " " , $d1 );		
			$d1Date = @explode( "-" , $d1[0] );
			//$d1Time = explode( ":" , $d1[1] );
			$d1 = @mktime( 0 , 0 , 0 , $d1Date[1] , $d1Date[2] , $d1Date[0] );

			$d2 = @explode( " " , $d2 );
			$d2Date = @explode( "-" , $d2[0] );
			//$d2Time = explode( ":" , $d2[1] );		
			$d2 = @mktime( 0 , 0 , 0 , $d2Date[1] , $d2Date[2] , $d2Date[0] );

				return @round(($d2-$d1)/(60*60*24));
			
			
			
		}
        
        public static function getModify( $modify , $date=false , $format="Y-m-d H:i:s" ){
            
            if(!$date) $date = date( $format );
            
            $time = new DateTime( $date );
            $time->modify( $modify );
                return $time->format( $format );
            
        }
        
        public static function secoundDiff( $olddate , $newdate ){
            return strtotime( $newdate )-strtotime( $olddate );
        }
    }
?>