<?php
require_once ('../inc/header.php');
require_once ('wfunctions.php');

if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
    require(__DIR__ . '/../vendor/autoload.php');
}
else {
    echo "autoload is not working";
    exit;
}

use phpWhois\Whois;
use phpWhois\Utils;

if(isset($_GET['q']) && $_GET['q']){
    $query = $_GET['q'];
    $query = removeString($query);
} else {
    echo "Error! Please Enter a Domain!";
    exit;
}

$title = "$query Whois Information - NameWest";
$othermeta = "<meta name=\"description\" content=\"Whois information for $query\">\n<meta name=\"keywords\" content=\"$query whois,whois data, whois\">";

generate_header($title, $othermeta);
generate_nav();

echo "<div class='container'><center><h1>Search another domain?</h1></center></div><br><br>";

generate_search_form();

// start collecting whois data

$conn = db_connect();
$q = "select domain from sites where domain = '$query'";
$res = $conn->query($q);

if(!isset($_GET['update']) && $res->num_rows>0){
    $data = "yes";
} else {
    
     $whois = new whois(); 
     $result = $whois->lookup($query);
     if($result['regrinfo']['registered'] != "unknown"){
         if($result['regrinfo']['registered'] != "no") {
// set domain info            
             $domain = $conn->escape_string($query);
// set registrar and referred info             
            if(isset($result['regrinfo']['domain']['sponsor'])){
              if(is_array($result['regrinfo']['domain']['sponsor'])){
                  $registrar = "";
                  foreach ($result['regrinfo']['domain']['sponsor'] as $key => $value) {
                    $registrar.= $conn->escape_string(trim($value));
                    $registrar.="<br/>";
                  }
              }else{
                  $registrar = $conn->escape_string($result['regrinfo']['domain']['sponsor']) ;
              }
              $referrer = "";
          }else{
              if(isset($result['regyinfo']['registrar'])) {
                  $registrar = $conn->escape_string($result['regyinfo']['registrar']) ;
              }  else {
                  $registrar = "";
              }
              
              if(isset($result['regyinfo']['referrer'])){
                  $referrer = $conn->escape_string($result['regyinfo']['referrer']) ;
              }  else {
                  $referrer = "";
              }
          }

//set domain status
          if(isset($result['regrinfo']['domain']['status'])){
            if (is_array($result['regrinfo']['domain']['status'])) {
              $status = "";
              foreach ($result['regrinfo']['domain']['status'] as $key => $value) {
                $status.= $conn->escape_string(trim($value));
                $status.=",";
              }
            }else{
              $status = $conn->escape_string($result['regrinfo']['domain']['status']);
            }
          }else{
              $status = "";
          }          
// set domain updated, created and expired time
          if (isset($result['regrinfo']['domain']['changed']) && $result['regrinfo']['domain']['changed'] != "") {
            $updated = $conn->escape_string(foramtDate($result['regrinfo']['domain']['changed'])) ;
          }else{
            $updated = "";
          }

          if (isset($result['regrinfo']['domain']['created']) && $result['regrinfo']['domain']['created'] != "") { 
            $created = $conn->escape_string(foramtDate($result['regrinfo']['domain']['created'])) ;
          }else{
            $created = "";
          }
  
          if (isset($result['regrinfo']['domain']['expires']) && $result['regrinfo']['domain']['expires'] != "") {
            $expires = $conn->escape_string(foramtDate($result['regrinfo']['domain']['expires'])) ;
          }else{
            $expires = "";
          }          
// set domain NS info
          if (isset($result['regrinfo']['domain']['nserver']) && $result['regrinfo']['domain']['nserver'] != "") {
            $nserver = "";
            foreach ($result['regrinfo']['domain']['nserver'] as $key => $value) {
              $nserver.= $conn->escape_string($key).":".$conn->escape_string($value).",";
            }
          }else{
            $nserver = "";
          }

// Get Alexa Rank and PR For the Domain
          $alexaRank = get_alexa_rank($query);
          $pageRank = getPRank($query);
         
// Get Domain Age
          if(isset($result['regrinfo']['domain']['created'])){
                $age = $result['regrinfo']['domain']['created'];
                if($age == ""){
                    $domainAge = "";
                }else{
                    $domainAge = domainAge($age);
                }
            }else{
                $domainAge = "";
            }    
    
// Fetch Raw data
          $rawData = "";
          foreach ($result['rawdata'] as $key => $value) {
            $rawData.= $conn->escape_string($value)."<br>";
          }

          $lastCheck = time();
          
          if(isset($_GET['update']) && $_GET['update'] == "true"){
              $q = "UPDATE sites SET registrar='$registrar', referralUrl='$referrer', status='$status', updated='$updated', created='$created', expires='$expires', nameservers='$nserver', alexaRank='$alexaRank', pageRank='$pageRank', domainAge='$domainAge', rawData='$rawData', lastCheck='$lastCheck'
            WHERE domain='$domain'";
              $res = $conn->query($q);
              if(!$res){
                  echo "Something was wrong to update domain information";
              }
              
          } else {
              $q = "INSERT INTO sites(domain, img, registrar, referralUrl, status, updated, created, expires, nameservers, alexaRank, pageRank, domainAge, rawData, views, lastCheck) 
            VALUES( '$domain', '', '$registrar', '$referrer', '$status', '$updated', '$created', '$expires', '$nserver', '$alexaRank', '$pageRank', '$domainAge', '$rawData', '1', '$lastCheck')";
              $res = $conn->query($q);
               if(!$res){
                  echo "Something was wrong to update domain information";
              }
              
          }
             
             
             
             
         } else {
             $available = "is available for registration.";
         }
         
     } else{
         $error = "Please sepcify a valid domain";
     }
     
    
}
?>

<div class="container">
<div class="row">
    <div class="col-md-10 col-md-offset-1 whois-container">
        <?php
          if(!isset($error) || $error == ""){
              if (!isset($available) || $available == ""){
                  $result = getData($query, $conn);
                  
                  if (isset($domainUpdated) && $domainUpdated == "true"){
                      echo "<div class=\"alert alert-success \">";
                      echo "Data Updated Successfully";
                      echo "</div>";
                  }
                  ?>
        <br><br>
  <div class="col-md-6 lead">Whois data for <strong><?php print_r($result['domain']) ;?></strong></div>
  <div class="col-md-6"><div class="pull-right">Last Updated On: <?php print_r(date("Y-m-d H:i:s A", $result['lastCheck'])."  ") ;?><a class="btn btn-success" href="<?php print_r($result['domain']) ;?>&update=true"><span class="glyphicon glyphicon-refresh"></span> Refresh Data</a></div></div>
  <div class="clearfix"></div>        
  <div class="row">
      <div class="col-md-5">

      <?php if ($result['registrar'] != "") { ?>
      <table class="table table-hover" style="max-width: 100%;">
        <thead>
          <tr>
            <th colspan="2">Registrar Info</th>
          </tr>
        </thead>
        <tbody>
          <?php if(isset($result['sponsor'])){ ?>

              <tr>
                <td>Name</td>
                <td><?php print_r($result['sponsor']) ;?></td>
              </tr>

          <?php } else{ ?>

              <?php if ($result['registrar'] != "") { ?>
              <tr>
                <td>Name</td>
                <td><?php print_r($result['registrar']) ;?></td>
              </tr>
              <?php } ?>
              <?php if ($result['referralUrl'] != "") { ?>
              <tr>
                <td>Referral Url</td>
                <td><?php print_r($result['referralUrl']) ;?></td>
              </tr>
              <?php }?>

          <?php } ?>

          <?php if($result['status'] != "") { ?>
          <tr>
            <td>Status</td>
            <td>
              <?php 
              $rstatus = explode(",", $result['status']);              
                foreach ($rstatus as $key => $value) {
                  if($value != "") {
                    echo trim($value);
                    echo "<br>";
                  }
                }
              ?>
            </td>
          </tr>
          <?php } ?>

        </tbody>
      </table>
      <?php } ?>

      <?php if ($result['updated'] != "" && $result['created'] != "" && $result['expires'] != "") { ?>
      <table class="table table-hover">
        <thead>
          <tr>
            <th colspan="2">Important Dates</th>
          </tr>
        </thead>
        <tbody>

          <?php if ($result['updated'] != "") { ?>
          <tr>
            <td>Updated:</td>
            <td><?php print_r($result['updated']) ;?></td>
          </tr>
          <?php } ?>

          <?php if ($result['created'] != "") { ?>
          <tr>
            <td>Created:</td>
            <td><?php print_r($result['created']) ;?></td>
          </tr>
          <?php } ?>

          <?php if ($result['expires'] != "") { ?>
          <tr>
            <td>Expires:</td>
            <td><?php print_r($result['expires']) ;?></td>
          </tr>
          <?php } ?>

        </tbody>
      </table>
      <?php } ?>

      <?php if ($result['nameservers'] != "") { ?>
      <table class="table table-hover"  style="max-width: 100%;">
        <thead>
          <tr>
            <th colspan="2">Name Servers</th>
          </tr>
        </thead>
        <tbody>
          <?php 

            $rnserver = explode(",", $result['nameservers']);

            reset($rnserver);
            foreach ($rnserver as $key => $value) {

              if($value != ""){
                $value = explode(":", $value);
                echo "<tr><td>";
                echo $value['0']."</td><td>".$value['1'];
                echo "</td></tr>";
              }
            }
          ?>
        </tbody>
      </table>
      <?php } ?>

     <table class="table table-hover">
        <thead>
          <tr>
            <th colspan="2">Site Stats</th>
          </tr>
        </thead>
        <tbody>

          <tr>
            <td>Alexa Rank:</td>
            <td><?php if($result['alexaRank'] == 0){ echo "No Data"; }else{ echo $result['alexaRank'];} ?></td>
          </tr>
          
          <tr>
            <td>Google Page Rank:</td>
            <td><?php print $result['pageRank'] ;?></td>
          </tr>

          <?php if($result['domainAge'] != "") { ?>
          <tr>
            <td>Domain Age:</td>
            <td><?php print $result['domainAge'] ;?></td>
          </tr>
          <?php } ?>

          <tr>
            <td>Total Views:</td>
            <td><?php print $result['views'] ;?></td>
          </tr>

        </tbody>
      </table>          
      </div>
      
      <div class="col-md-7 well">
      <h5>Raw Registrar Data</h5>
      <p>
        <?php 

          echo $result['rawData'];
        ?>
      </p>
    </div>
  </div>
        
                  
                  
        <?php          
              } else {
                  echo "<div class=\"alert alert-success \">";
                  echo $query." ".$available."<br>";
                  echo "</div>";
              }
              
              
          } else {
              echo "<div class=\"alert alert-block\">";
              echo "<h4>ERROR!</h4>";
              echo $error."<br>";
              echo "</div>";
              
          }


        
        ?>
        
        
        
    </div>
    </div>
</div>


<div class="clearfix"></div>

<?php
generate_footer();
?>
