<?php 
define("FPDF_FONTPATH", "font/");
define("ORIG_SIDN_PDF", "./contractdomeinnaam.pdf"); // het orig. pdf file (contract)
include($DOCUMENT_ROOT."/fpdf/fpdi.php"); // de locatie waar de fpdf bestanden moeten staan (je kan deze ook wijzigen)
class SIDN_contract extends fpdi {

	var $domain;
	
	var $owner;
	var $owner_address;
	var $owner_pc_city_country;
	var $owner_nl_address;
	
	var $admin_name;
	var $admin_phone;
	var $admin_email;
	
	var $tech_name;
	var $tech_phone;
	var $tech_email;
	
	var $language = "nl"; 
	var $message = array();
	
	// this method is used to handle tasks like open (import) the standaard pdf file
	// and creating all the text fields by usinf the create_text_field() method.
	// At least the method output_pdf() is called to create the new PDF
    function create_data() {
        $this->import_pdf();
        $this->create_logo();
        $this->SetTextColor(0,0,0);
        $this->create_text_field($this->domain, 65, 43, 100);
        $this->create_text_field($this->owner, 65, 58, 100);
        $this->create_text_field($this->owner_address, 65, 65, 100);
        $this->create_text_field($this->owner_pc_city_country, 65, 72, 100);
        $this->create_text_field($this->owner_nl_address, 65, 79, 100);        
        $this->create_text_field($this->admin_name, 65, 93, 100);
        $this->create_text_field($this->admin_phone, 65, 100, 40);
        $this->create_text_field($this->admin_email, 113, 100, 50);
        $this->create_text_field($this->tech_name, 65, 107, 100);
        $this->create_text_field($this->tech_phone, 65, 114, 40);
        $this->create_text_field($this->tech_email, 113, 114, 50);
        $this->create_text_field($this->create_date(), 43, 214, 50);
        $this->output_pdf();
    }
	// first the domain name string is checked according existing rules 
	function check_domain_name() {
		if ( $this->domain == "") {
			$this->message[] = $this->message_strings(9);
			return false;
		} else {
			if (preg_match("/^([A-Za-z0-9]+(\-?[A-za-z0-9]*)){2,63}$/", $this->domain)) {
				if ($this->get_whois_data()) {
					$this->message[] = $this->message_strings(13);
				} else {
					$this->message[] = $this->message_strings(10);
				}
			} else {
				$this->message[] = $this->message_strings(11);
				return false;
			}
		}
	}
	// this method is used to query SIDN's whois database
	function get_whois_data() {
		$buffer = "";
		$connection = @fsockopen("whois.domain-registry.nl", 43, $errno, $errstr, 10);
		if (!$connection) {
			$this->message[] = $this->message_strings(12);
			return;
		} else {
			sleep(2);
			fputs($connection, $this->domain.".nl\r\n"); 
			while (!feof($connection)) {
				$buffer .= fgets($connection, 4096);
			}
			fclose($connection);
		}
		if (eregi("is free", $buffer)) {
			return true;
		} else {
			return false;
		}
	}

	// use this method to handle all (error)messages in diff languages
	function message_strings($num) {
		switch ($this->language) {
			case "nl":
			$msg[9] = "Het veld \"Domeinnaam\" is leeg, geef een naam op om te testen.";
			$msg[10] = "Helaas, deze domeinnaam is al bezet, probeer een andere.";
			$msg[11] = "De opgeven domeinnaam is niet geldig, alleen letter's en cijfers zijn toegestaan.";
			$msg[12] = "Kan op het moment geen verbinging met de WHOIS server maken, <br>probeer het later nog een keer.";
			$msg[13] = "De opgeven domeinnaam is vrij.";
			break;
		}
		return $msg[$num];
	}
	// use this method to create one message string from the message array
	function show_msg_string() {
		$msg_string = "";
		foreach ($this->message as $value) {
			$msg_string .= $value."<br>\n";
		}
		return $msg_string;
	}
	// this method is used to create a dutch styled date notation
	function create_date() {
		$nl_month = array("1" => "januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september", "october", "november", "december");
		if ($this->language == "nl") {
			$date_str = date("d")." ";
			$date_str .= $nl_month[intval(date("m"))]." ";
			$date_str .= date("Y");
		} else { 
			$date_str = date("d-m-Y");
		}
		return $date_str;
	}
	// this method is a group of functions (from fpdi) to open/import an existing pdf file
	function import_pdf() {
		$this->setSourceFile(ORIG_SIDN_PDF);
		$page = $this->ImportPage(1);
		$this->AddPage();
		$this->useTemplate($page);
	}
	// these three fpdf function create a text cell at specified position
	function create_text_field($string, $x, $y, $width, $height = 2) {
		$this->SetXY($x,$y);
		$this->Setfont("Arial","",10);
		$this->Cell($width, $height, $string, 0, 0, "");
	}
	// and at least the output command, the second header is used to resolve cache problems
	function output_pdf() {
		header("Content-type: application/pdf");
		header ("Cache-control: private"); 
		$this->Output($this->domain.".pdf", "I");
	}
}

// gebruik deze code samen met een formulier voor alle variabelen
$contract = new SIDN_contract; // create a new object

if (isset($_POST['submit'])) {
	$contract->domain = $_POST['domein']; // without the tld (.nl)
	if ($_POST['submit'] == "Controleer") {
		$contract->check_domain_name(); // domain check only
	} else {
		// next define the values foor all variables. You can use a result from the database or posted by a form
		$contract->owner = $_POST['owner'];
		$contract->owner_address = $_POST['adres'];
		$contract->owner_pc_city_country = $_POST['pc_plaats'];
		$contract->owner_nl_address = $_POST['adres_nl'];
			
		$contract->admin_name = $_POST['admin_name'];
		$contract->admin_phone = $_POST['admin_tel'];
		$contract->admin_email = $_POST['admin_mail'];
			
		$contract->tech_name = $_POST['tech_name'];
		$contract->tech_phone = $_POST['tech_tel'];
		$contract->tech_email = $_POST['tech_mail'];
		
		// example code with mysql (just in theorie)
		// $result = mysql_query("SELECT * FROM nl_table WHERE cust_id = 12"); 
		// $fields = mysql_tech_object($result);
		// $contract->domain = $fields->the_domein;
		// $contract->owner = $fields->the_owner;
		// ...
		
		$contract->create_data();
		flush();
		// the example test / form is in dutch language (because I think that dutch people will use it
	// if you need assistance then contact me
	} 
}
echo $contract->show_msg_string(); // print hier de messages
?>