<?php
        include('extensions.php');
        //error_reporting(E_ALL); //Enable to show error on this script when what you see is white page.

        //DB configurations.
        $dbhost = '192.168.100.254';
        $dbname = 'registryasp';
        $dbuser = 'urlfwd';
        $dbpassword = '0Oav7Le0GCWZqmCu1osDQDxCblbQe1';

        //GET the domain that people trying to reach.
        $incoming_domain = $_SERVER["HTTP_HOST"];

        // Force all domain to have "www." in front of the domain.
        //if(strpos($incoming_domain, 'www.') === false) {
        //      $incoming_domain = "www.".$incoming_domain;
        //}

        //Break the domain into sub domain, domain and extension.
        $fqdn = explode ('.', $incoming_domain);

        //check if it is 3LD or 4LD
        $dlength = count($fqdn);
        switch($dlength) {
                case 2: //actually, this 2LD domain will never happened since we for to add "www." in front of each incoming domain.
                        $subdomain = "www";
                        $domain = $fqdn[0];
                        $extension = $fqdn[1];
                        break;
                case 3:
                        $ext = $fqdn[1].".".$fqdn[2];
                        //echo $ext;
                        if(in_array($ext, $exts)) {
                                $subdomain = "www";
                                $domain = $fqdn[0];
                                $extension = $fqdn[1]. "." .$fqdn[2];
                        } else {
                                $subdomain = $fqdn[0];
                                $domain = $fqdn[1];
                                $extension = $fqdn[2];
                        }
                        break;
                case 4:
                        $subdomain = $fqdn[0];
                        $domain = $fqdn[1];
                        $extension = $fqdn[2].".".$fqdn[3];
                        break;
                case 5:
                        $subdomain = $fqdn[1];
                        $domain = $fqdn[2];
                        $extension = $fqdn[3].".".$fqdn[4];
                        break;
                default:
                        echo "Invalid domain.";
                        break;
        }
        //Debug what is the value for the FQDN.
        //echo "<br />subdomain = " . $subdomain . "<br />";
        //echo "domain = " . $domain . "<br />";
        //echo "extension = " . $extension . "<br />";

        // DB connection.
        $db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpassword, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"));
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //set DB connection to show error message if any.

        //SQL to query Domain ID, did.
        $sql = "select url from tbl_urlfwd where subdom = \"$subdomain\" and did = (select did from tbl_domain where domain = \"$domain\" and ext = \"". idn_to_utf8($extension)."\")";
        //echo $sql; //Debug SQL command
        //get the Domain ID from DB
        try {
                $query = $db->query($sql);
                $query->setFetchMode(PDO::FETCH_ASSOC);
                $result = $query->fetch();
        } catch (PDOException $pe) {
                die("Query error :" . $pe->getMessage());
        }

        $db = NULL; // Clear the DB query and release the memory used.
        //if there is result for the URL Forward, send the client to the page.
        if ($result['url']) {
                echo $result['url']; //use to debug what is the value return by DB.
                if(strpos($result['url'],'http://') !== false || strpos($result['url'], 'https://') !== false) {
                        $destination = "Location: ".$result['url'];
                        header("$destination",true,301);
                        echo $destination; //Comment the line above and uncomment this line to show what is the value retrieved from DB.
                } else {
                        $destination = "Location: http://".$result['url'];
                        header("$destination",true,301);
                        echo $destination; //Comment the line above and uncomment this line to show what is the value retrieved from DB.
                }
        }
?>
