----- knvbupdater.class.php -----
<?php
/******************************************
*************KNVB CLASS v3.0.0*************
*******Laatste update: 16-09-2007**********
*******(C) by Remco van Noorloos***********
***Dit script mag door iedereen en overal** 
**gebruikt worden mits deze melding intact* 
*****************blijft.*******************
*******************************************
*****Voor opmerkingen en/of commentaar:****
************remcovn@gmail.com**************
******************************************/

require_once("wedstrijd.bo.class.php");
require_once("club.bo.class.php");
require_once("standregel.bo.class.php");

/**
 * Met behulp van deze class kun je diverse informatie van knvb.nl afhalen en 
 * makkelijk interpreteren in je eigen website-layout.
 */
class KnvbUpdater
{
	private $sUsername;
	private $sPassword;
	private $aErrorLog = array();
	
	private static $sVersion = "3.0.0";
	
	public function __construct($Username, $Password)
	{
		$this->sUsername = $Username;
		$this->sPassword = $Password;
	}
	
	/**
	 * Geeft huidige versie terug
	 *
	 * @return string
	 */
	public function getVersion()
	{
		return KnvbUpdater::$sVersion;
	}
	
	/**
	 * Geeft de error-log terug
	 *
	 * @return array
	 */
	public function getErrorLog()
	{
		return $this->aErrorLog;
	}
	
	/**
	 * Haal de stand op met behulp van een resource
	 *
	 * @param string $resource
	 * @return array
	 */
	public function getStand($resource)
	{		
		$connectknvb = $this->connectknvb($resource);
		if(!$connectknvb)
		{
			$this->reportError("101", $resource, date("d-m-Y H:i:s"), $_SERVER['REMOTE_ADDR'], gethostbyaddr($_SERVER['REMOTE_ADDR']));
			return false;
		}
		else
		{
			$exp1 = explode('<table border="0" cellpadding="4" cellspacing="0" width="100%" class="score">', $connectknvb);
			$standstr = $exp1[3];
			$standstr = explode('</table></td>', $standstr);
			$standstr = str_replace('</td>', '', $standstr);
			$exp2 = explode('</tr>', $standstr[0]);
			$count = count($exp2)-1;
			
			for($i = 1; $i < $count; $i++)
			{
				$exp3 = explode('<td align="center" class="scoretd">', $exp2[$i]);
				$exp4 = explode('<td align="left" class="scoretd">', $exp3[0]);
				
				$StandRegel = 
					new StandRegel(
						$i, str_replace("</b>", "", str_replace("<b>", "", trim($exp4[2]))),
						trim($exp3[1]), trim($exp3[2]), trim($exp3[3]), trim($exp3[4]),
						str_replace("</b>", "", str_replace("<b>", "", trim($exp3[5]))),
						trim($exp3[6]), trim($exp3[7]), trim($exp3[8])
					);
				$stand[] = $StandRegel;
			}
			
			return $stand;
		}
	}
	
	/**
	 * Haal het volledige programma van een club op met behulp van een resource
	 *
	 * @param string $resource
	 * @return array
	 */
	public function getClubProgramma($resource)
	{
		$connectknvb = $this->connectknvb($resource);
		if(!$connectknvb)
		{
			$this->reportError("101", $resource, date("d-m-Y H:i:s"), $_SERVER['REMOTE_ADDR'], gethostbyaddr($_SERVER['REMOTE_ADDR']));
			return false;
		}
		else
		{
			$exp1 = explode('<TABLE cellSpacing=2 cellPadding=4 width="100%" border=0 >', $connectknvb);
			$exp1 = $exp1[1] . $exp1[2];
			
			$progstring = str_replace('<td class="coachtd">', '', $exp1);
			$progstring = str_replace('<td class="coachtd" align="center">', '', $progstring);
			$exp2 = explode('<TR class="trlb">', $progstring);
			
			for($i = 1; $i < count($exp2); $i++)
			{
				$Wedstrijd = new Wedstrijd();
				
				$exp3 = explode('</tr>', $exp2[$i]);
				$exp4 = explode('</td>', $exp3[0]);
				
				$datum = trim($exp4[0]);
				$datumexpl = explode("/", $datum);
				
				$Wedstrijd->setDatum($datumexpl[2] . "-" . $datumexpl[1] . "-" . $datumexpl[0]);
				$Wedstrijd->setAanvang(trim($exp4[1]));
				
				$wedstrijd = trim($exp4[3]);
				$wedstrijdexpl = explode(" - ", $wedstrijd);
				
				$Wedstrijd->setScheidsrechter(trim($exp4[4]));
				$Wedstrijd->setThuisTeam(trim(str_replace("(zat)", "", $wedstrijdexpl[0])));
				$Wedstrijd->setUitTeam(trim(str_replace("(zat)", "", $wedstrijdexpl[1])));
				
				if(strlen( $Wedstrijd->getThuisTeam() ) > 0)
				{
					$programma[] = $Wedstrijd;
				}
			}
			
			return $programma;
		}
	}
	
	/**
	 * Haal de uitslagen op met behulp van een resource
	 *
	 * @param string $resource
	 * @return array
	 */
	public function getUitslagen($resource)
	{
		$connectknvb = $this->connectknvb($resource);
		if(!$connectknvb)
		{
			$this->reportError("101", $resource, date("d-m-Y H:i:s"), $_SERVER['REMOTE_ADDR'], gethostbyaddr($_SERVER['REMOTE_ADDR']));
			return false;
		}
		else
		{
			$exp1 = explode('<table border="0" cellpadding="4" cellspacing="0" width="100%" class="score">', $connectknvb);
			
			$uitslagstr = $exp1[2];
			$uitslagstr = str_replace('', '', $uitslagstr);
			$exp2 = explode('<table border="0" cellpadding="0" cellspacing="0">', $uitslagstr);
			$exp3 = explode('</tr>', $exp2[0]);
			
			for($i = 1; $i < count($exp3)-2; $i++)
			{
				$Wedstrijd = new Wedstrijd();
				
				$string = str_replace('</td>', '', $exp3[$i]);
				$string = str_replace('<b>', '', $string);
				$string = str_replace('</b>', '', $string);
				$string = str_replace('&nbsp;', '', $string);
				$exp4 = explode('<td class="scoretd">', $string);
				
				$datum = $exp4[1];
				$datumexp = explode("-", $datum);
				
				$Wedstrijd->setDatum(trim($datumexp[2]) . "-" . trim($datumexp[1]) . "-" . trim($datumexp[0]));
				$Wedstrijd->setThuisTeam( trim($exp4[2]) );
				$Wedstrijd->setUitTeam( trim($exp4[3]) );			
				
				$uitslag = trim($exp4[4]);
				$uitslagexp = explode(" - ", $uitslag);
				$Wedstrijd->setUitslag( $uitslagexp[0] . "-" . $uitslagexp[1] );
				
				$uitslagen[] = $Wedstrijd;
			}
			
			return $uitslagen;
		}
	}
	
	/**
	 * Haal het speelschema op met behulp van een resource
	 *
	 * @param string $resource
	 * @return array
	 */
	public function getSpeelSchema($resource)
	{
		$connectknvb = $this->connectknvb($resource);
		if(!$connectknvb)
		{
			$this->reportError("101", $resource, date("d-m-Y H:i:s"), $_SERVER['REMOTE_ADDR'], gethostbyaddr($_SERVER['REMOTE_ADDR']));
			return false;
		}
		else
		{
			$exp1 = explode('<table border="0" cellpadding="4" cellspacing="0" width="100%" class="score">', $connectknvb);
			$exp2 = explode('<table border="0" cellpadding="2" cellspacing="2" width="100%" class="score">', $exp1[1]);
			for($i = 1; $i < count($exp2); $i++)
			{
				$exp3 = explode('<td align="left" width="50%">', $exp2[$i]);
				for($y = 1; $y < count($exp3); $y++)
				{
					$Wedstrijd = new Wedstrijd();
					
					$exp4 = explode('</td>', $exp3[$y]);
					$wedstrijd = str_replace('<tr class="clubselectie">', '', $exp4[0]);
					$wedstrijd = str_replace('<tr>', '', $wedstrijd);
					$wedstrijd = str_replace('<td class="scoretd" colspan="8"><b>Wedstrijd 1</b><br/>', '', $wedstrijd);
					$wedstrijdexp = explode(' - ', $wedstrijd);
					
					$Wedstrijd->setThuisTeam( $wedstrijdexp[0] );
					$Wedstrijd->setUitTeam( $wedstrijdexp[1] );
					
					$speelschema[$i][$y] = $Wedstrijd;
				}
			}
			return $speelschema;
		}
	}
	
	/**
	 * Haal de uitslagen op met behulp van een resource
	 *
	 * @param string $resource
	 * @return array
	 */
	public function getCompetitieIndeling($resource)
	{
		$connectknvb = $this->connectknvb($resource);
		if(!$connectknvb)
		{
			$this->reportError("101", $resource, date("d-m-Y H:i:s"), $_SERVER['REMOTE_ADDR'], gethostbyaddr($_SERVER['REMOTE_ADDR']));
			return false;
		}
		else
		{
			$exp1 = explode('<table border="0" cellpadding="4" cellspacing="0" width="100%" class="score">', $connectknvb);
			$exp2 = explode('<td align="center" width="100" style="background-color: White;"><b>Voorkeurstijd</b></td>', $exp1[0]);
			$exp3 = explode('</table></td>', $exp2[1]);
			$exp4 = explode('<tr class="clubselectie">', $exp3[0]);
			
			for($i = 1; $i < count($exp4); $i++)
			{
				$exp5 = explode('</td>', $exp4[$i]);
				
				$club = str_replace('<td>', '', $exp5[1]);
				$tijd = str_replace('<td align="center">', '', $exp5[2]);
				$tijd = str_replace('</tr>', '', $tijd);
				
				$Club = new Club( trim($club), trim($tijd) );
				
				$indeling[] = $Club;
			}
			
			return $indeling;
		}
	}
	
	/**
	 * Haal alle uitslagen op met behulp van de opgegeven resource
	 *
	 * @param string $resource
	 * @return array
	 */
	public function getAlleUitslagen($resource)
	{
		$connectknvb = $this->connectknvb($resource);
		if(!$connectknvb)
		{
			$this->reportError("101", $resource, date("d-m-Y H:i:s"), $_SERVER['REMOTE_ADDR'], gethostbyaddr($_SERVER['REMOTE_ADDR']));
			return false;
		}
		else
		{
			$exp1 = explode('<tr class="scoreheader">', $connectknvb);
			for($i = 1; $i < count($exp1); $i++)
			{
				$exp2 = explode('<td align="center"', $exp1[$i]);
				for($y = 1; $y < count($exp2); $y++)
				{
					$sTemp = str_replace('class="matrixWhite" title="', '', $exp2[$y]);
					$sTemp = str_replace('class="matrixGreen', '', $sTemp);
					$exp3 = explode('">', $sTemp);
					$exp4 = explode('</tr>', $exp3[1]);
					$exp5 = explode('><TABLE height=60 cellSpacing=0 cellPadding=0 width=468 border=0>', $exp3[0]);
					
					if(strlen(trim($exp5[0])))
					{
						$Wedstrijd = new Wedstrijd();
						
						$wedstrijdexp = explode(" - ", trim($exp5[0]));
						if(trim(str_replace('</td>', '', $exp4[0])) == "*")
						{
							$uitslag = "";
						}
						else
						{
							$uitslag = trim(str_replace('</td>', '', $exp4[0]));
						}
						$Wedstrijd->setUitslag($uitslag);
						
						if( strlen($wedstrijdexp[0]) > 0 && strlen($wedstrijdexp[1]) > 0)
						{
							$Wedstrijd->setThuisTeam( $wedstrijdexp[0] );
							$Wedstrijd->setUitTeam( $wedstrijdexp[1] );
							$uitslagen[] = $Wedstrijd;
						}
					}
				}
			}
			
			return $uitslagen;
		}
	}
	
	/**
	 * Haalt het programma van een bepaalde competitie op
	 *
	 * @param string $resource
	 * @return array
	 */
	public function getCompProgramma($resource)
	{
		$connectknvb = $this->connectknvb($resource);
		if(!$connectknvb)
		{
			$this->reportError("101", $resource, date("d-m-Y H:i:s"), $_SERVER['REMOTE_ADDR'], gethostbyaddr($_SERVER['REMOTE_ADDR']));
			return false;
		}
		else
		{
			$connectknvb = str_replace('<tr class="clubselectie">', '<tr>', $connectknvb);
			$exp1 = explode('<tr class="scoreheader">', $connectknvb);
			$exp2 = explode('</table></td>', $exp1[3]);
			$exp3 = explode('<tr>', $exp2[0]);
			for($i = 1; $i < count($exp3); $i++)
			{
				$Wedstrijd = new Wedstrijd();
				
				$sString = str_replace('</td>', '', $exp3[$i]);
				$sString = str_replace('<td class="scoretd" nowrap="nowrap">', '', $sString);
				$sString = str_replace('</tr>', '', $sString);
				$exp4 = explode('<td class="scoretd">', $sString);
				
				$wedstrijdexp = explode(" - ", trim(str_replace("</b>", "", str_replace("<b>", "", $exp4[2]))));
				$Wedstrijd->setDatum( trim($exp4[0]) );
				$Wedstrijd->setAanvang( trim($exp4[1]) );
				
				$Wedstrijd->setThuisTeam( $wedstrijdexp[0] );
				$Wedstrijd->setUitTeam( $wedstrijdexp[1] );
				
				
				$programma[] = $Wedstrijd;
			}
			
			return $programma;
		}
	}
	
	/**
	 * Haalt de periode-standen op met behulp van de opgegeven resource
	 *
	 * @param string $resource
	 * @return array
	 */
	public function getPeriodeStand($resource)
	{
		$connectknvb = $this->connectknvb($resource);
		if(!$connectknvb)
		{
			$this->reportError("101", $resource, date("d-m-Y H:i:s"), $_SERVER['REMOTE_ADDR'], gethostbyaddr($_SERVER['REMOTE_ADDR']));
			return false;
		}
		else
		{
			$exp1 = explode('<tr class="scoreheader">', $connectknvb);
			
			$periode = 1;
			for($i = 4; $i < count($exp1); $i++)
			{
				$exp2 = explode('<td align="right" valign="middle" style="padding: 4px;">', $exp1[$i]);
				$sTemp = str_replace('<td align="left" class="scoretd">', '', $exp2[0]);
				$sTemp = str_replace('<td align="center" class="scoretd">', '', $sTemp);
				$sTemp = str_replace('<tr class="clubselectie">', '<tr>', $sTemp);
				$exp3 = explode('<tr>', $sTemp);
				for($y = 1; $y < count($exp3); $y++)
				{
					$exp4 = explode('</td>', $exp3[$y]);
					
					if(strlen($exp4[1])!=0)
					{
						$StandRegel = 
							new StandRegel(
								$y, trim(str_replace("</b>", "", str_replace("<b>", "", $exp4[1]))),
								trim($exp4[2]), trim($exp4[3]), trim($exp4[4]), trim($exp4[5]),
								str_replace("</b>", "", str_replace("<b>", "", trim($exp4[6]))),
								trim($exp4[7]), trim($exp4[8]), trim($exp4[9])
							);
						$stand[$periode][] = $StandRegel;
					}
				}
				$periode++;
			}
			
			return $stand;
		}
	}
	
	/**
	 * Zet array om naar xml-indeling
	 *
	 * @param array $resource
	 * @return string
	 */
	public function write2xml($resource, $type = "default")
	{
		header("Content-type: text/xml");
		$xml = "<knvbupdater version=\"" . $this->sVersion . "\">";
		if($type == "speelSchema")
		{
			$i = 1;
			foreach($resource as $array)
			{
				$xml .= "<speelronde number=\"" . $i . "\">";
				foreach($array as $key => $value)
				{
					$xml .= "<wedstrijd>" . htmlentities($value) . "</wedstrijd>";
				}
				$xml .= "</speelronde>";
				$i++;
			}
		}
		elseif($type == "default")
		{
			for($i = 0; $i < count($resource); $i++)
			{
				$xml .= "<item>";
				foreach($resource[$i] as $key => $value)
				{
					$xml .= "<" . htmlentities($key) . ">" . htmlentities($value) . "</" . $key . ">";
				}
				$xml .= "</item>";		
			}
		}
		$xml .= "</knvbupdater>";
		
		return $xml;
	}
	
	
	/**
	 * Breng een verbinding tot stand met knvb.nl
	 *
	 * @return string
	 */
	private function connectknvb($resource)
	{
		if(strlen($resource)!=0)
		{
			if($this->verifyUser($this->sUsername, $this->sPassword) == "")
			{
				return false;
			}
			else
			{
				if($this->checkCurl())
				{
					$ch = curl_init();   
					curl_setopt($ch, CURLOPT_URL, $resource);
					curl_setopt($ch, CURLOPT_COOKIE, "sessionId=" . $this->verifyUser($this->sUsername, $this->sPassword));
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
					$file = curl_exec($ch);
					curl_close ($ch);	
				}

				return $file;
			}
		}
		else
		{
			return false;
		}
	}

	/**
	 * Functie om in te loggen met opgegeven gebruikersnaam en wachtwoord
	 * 
	 * @return string
	 */
	private function verifyUser($sUsername, $sPassword)
	{
		if($this->checkCurl())
		{
			$URL = "http://www.knvb.nl/mijn?cc_req=1;next=bla";
			$ch = curl_init();   
			curl_setopt($ch, CURLOPT_URL, $URL); 
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, "method=login&email=" . $sUsername . "&password=" . $sPassword . "&Login=Login");
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			$string = curl_exec($ch);
			curl_close ($ch);
		}

		$stringexplode = explode('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">', $string);
		$sessionId = substr($stringexplode[0], strpos($stringexplode[0], "sessionId=")+10, 32);
		
		return $sessionId;
	}
	
	/**
	 * Controleert of de CURL library geinstalleerd is
	 *
	 * @return boolean
	 */
	private function checkCurl()
	{
		return function_exists("curl_init");
	}
	
	/**
	 * Foutmelding aan log toevoegen
	 *
	 * @param array $errorInformation
	 */
	private function reportError($errorInformation)
	{
		$aErrorLog[] = array("errorno" => $errorInformation[0], "error" => $errorInformation[1], "timestamp" => $errorInformation[2], "ip" => $errorInformation[3], "host" => $errorInformation[4]);
	}
}
?>



----- club.bo.class.php -----

<?php
class Club
{
	private $sClub;
	private $sVoorkeursTijd;
	
	public function __construct
		(
			$Club = "", $VoorkeursTijd = ""
		)
	{
		$this->sClub = $Club;
		$this->sVoorkeursTijd = $VoorkeursTijd;
	}
	
	public function getClub()
	{
		return $this->sClub;
	}
	
	public function getVoorkeursTijd()
	{
		return $this->sVoorkeursTijd;
	}
	
	public function setClub($Club)
	{
		$this->sClub = $Club;
	}
	
	public function setVoorkeursTijd($VoorkeursTijd)
	{
		$this->sVoorkeursTijd = $VoorkeursTijd;
	}
}
?>



----- standregel.bo.class.php -----

<?php
class StandRegel
{
	private $iPositie;
	private $sTeam;
	private $iGespeeld;
	private $iGewonnen;
	private $iGelijk;
	private $iVerloren;
	private $iPunten;
	private $iDoelVoor;
	private $iDoelTegen;
	private $iPuntenMindering;
	
	public function __construct
		(
			$Positie, $Team, $Gespeeld, $Gewonnen, $Gelijk, $Verloren, $Punten,
			$DoelVoor, $DoelTegen, $PuntenMindering
		)
	{
		$this->iPositie = $Positie;
		$this->sTeam = $Team;
		$this->iGespeeld = $Gespeeld;
		$this->iGewonnen = $Gewonnen;
		$this->iGelijk = $Gelijk;
		$this->iVerloren = $Verloren;
		$this->iPunten = $Punten;
		$this->iDoelVoor = $DoelVoor;
		$this->iDoelTegen = $DoelTegen;
		$this->iPuntenMindering = $PuntenMindering;
	}
	
	public function getPositie()
	{
		return $this->iPositie;
	}
	
	public function getTeam()
	{
		return $this->sTeam;
	}
	
	public function getGespeeld()
	{
		return $this->iGespeeld;
	}
	
	public function getGewonnen()
	{
		return $this->iGewonnen;
	}
	
	public function getGelijk()
	{
		return $this->iGelijk;
	}
	
	public function getVerloren()
	{
		return $this->iVerloren;
	}
	
	public function getPunten()
	{
		return $this->iPunten;
	}
	
	public function getDoelVoor()
	{
		return $this->iDoelVoor;
	}
	
	public function getDoelTegen()
	{
		return $this->iDoelTegen;
	}
	
	public function getPuntenMindering()
	{
		return $this->iPuntenMindering;
	}
}
?>


----- wedstrijd.bo.class.php -----

<?php
class Wedstrijd
{
	private $sDatum;
	private $sAanvang;
	private $sThuisTeam;
	private $sUitTeam;
	private $sUitslag;
	private $sScheidsrechter;
	
	public function __construct
		(
			$Datum = "", $Aanvang = "", 
			$ThuisTeam = "", $UitTeam = "", $Uitslag = "",
			$Scheidsrechter = ""
		)
	{
		$this->sDatum = $Datum;
		$this->sAanvang = $Aanvang;
		$this->sThuisTeam = $ThuisTeam;
		$this->sUitTeam = $UitTeam;
		$this->sUitslag = $Uitslag;
		$this->sScheidsrechter = $Scheidsrechter;
	}
	
	public function getDatum()
	{
		return $this->sDatum;
	}
	
	public function getAanvang()
	{
		return $this->sAanvang;
	}
	
	public function getThuisTeam()
	{
		return $this->sThuisTeam;
	}
	
	public function getUitTeam()
	{
		return $this->sUitTeam;
	}
	
	public function getUitslag()
	{
		return $this->sUitslag;
	}
	
	public function getScheidsrechter()
	{
		return $this->sScheidsrechter;
	}
	
	public function setDatum($Datum)
	{
		$this->sDatum = $Datum;
	}
	
	public function setAanvang($Aanvang)
	{
		$this->sAanvang = $Aanvang;
	}
	
	public function setThuisTeam($ThuisTeam)
	{
		$this->sThuisTeam = $ThuisTeam;
	}
	
	public function setUitTeam($UitTeam)
	{
		$this->sUitTeam = $UitTeam;
	}
	
	public function setUitslag($Uitslag)
	{
		$this->sUitslag = $Uitslag;
	}
	
	public function setScheidsrechter($Scheidsrechter)
	{
		$this->sScheidsrechter = $Scheidsrechter;
	}
	
	public function getWedstrijd()
	{
		return $this->sThuisTeam . "-" . $this->sUitTeam;
	}
}
?>
