<?php
if(isset($_POST['link'])) {
    $link = $_POST['link'];

    //remove protocol, we're assuming its http
    if(strstr($link, '://') != false){
        $link = substr($link, strpos($link,'://') + strlen('://'));
    }

    //split the url into host (www.example.com) and requested page (index.php) on the first slash
    $host = substr($link, 0, strpos($link, '/'));
    $request = substr($link, strpos($link, '/'));

    //open a socket connection to the site on port 80 (http protocol port)
    $fp = @fsockopen($host, 80, $errno, $errstr);

    if($fp == false){
        $response = $errno . " " . $errstr;
    }
    else {
        //create the request headers
        $headers   = array();
        $headers[] = 'HEAD ' . $request . ' HTTP/1.1';
        $headers[] = 'Host: ' . $host;
        $headers[] = 'Content-Length: 0';
        $headers[] = 'Connection: close';
        $headers[] = 'Content-Type: text/html';
        $headers   = implode("\r\n", $headers) . "\r\n\r\n";

        //send our request
        if (!fwrite($fp, $headers)) {
            fclose($fp);
            $response = "Could not access webpage specified.";
        }
        else {
            //read the response
            $rsp = '';
            while(!feof($fp)) { $rsp .= fgets($fp,8192); }
            fclose($fp);
            $hunks = explode("\r\n\r\n",trim($rsp));
            $headers = explode("\n", $hunks[count($hunks) - 1]);
            $response = trim($headers[0]);
        }
    }
}
?>

<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Katalog</title>
    <link rel="stylesheet" type="text/css" href="scss/style.css" media="screen" />
</head>
<body>
    <div class="Titel">
        <a href="index.html"><button id= "zurueck">zurück</button></a>
        <h1>Katalog</h1>
    </div>

    <div class="Eingabe">
        <h2>Eingabe</h2>
        <form method="POST">
            <input type="text" name="Title" class="input" placeholder="Titel">
            <input type="text" name="Author" class="input" placeholder="Autor">
            <input type="text" name="Country" class="input" placeholder="Herkunft">
            <input type="url" name="Link" class="input" placeholder="Link">
            <input type="text" name="Language" class="input" placeholder="Sprache">
            <input type="number" name="Pages" class="input" placeholder="Seiten" min="0">
            <input type="number" name="Year" class="input" placeholder="Erscheinungsjahr" min="0">
            <select class="input" name="Category">
                <option>Book</option>
                <option>Magazin</option>
                <option>EBook</option>
                <option>EPaper</option>
            </select>
            <input type="url" name="ImageLink" class="input" placeholder="Bild">
            <input type="number" name="LendTime" class="input" placeholder="Verteihdauer" min="0">
            <select class="input" name="Category">
                <option>Physical</option>
                <option>Virtual</option>
            </select>
            <input type="submit" name="go" id="go">
        </form>
        <?php if(isset($response)) {  echo $response;  } ?>
    </div>
    <div class="Ausgabe">
        <h2>Ausgabe</h2>
        <table>
            <tr>
                <td class ="fix">ProductId</td>
                <td></td>
            </tr>
            <tr>
              <td class="fix">Titel</td>
              <td></td>
            </tr>
            <tr>
                <td class="fix">Autor</td>
                <td></td>
            </tr>
            <tr>
                <td class="fix">Land</td>
                <td></td>
            </tr>
            <tr>
                <td class="fix">Link</td>
                <td></td>
            </tr>
            <tr>
                <td class="fix">Sprache</td>
                <td></td>
            </tr>
            <tr>
                <td class="fix">Seitenzahl</td>
                <td></td>
            </tr>
            <tr>
                <td class="fix">Erscheinungsjahr</td>
                <td></td>
            </tr>
            <tr>
                <td class="fix">Kategorie</td>
                <td></td>
            </tr>
            <tr>
                <td class="fix">Bildlink</td>
                <td></td>
            </tr>
            <tr>
                <td class="fix">Verleihdauer</td>
                <td></td>
            </tr>
            <tr>
                <td class="fix">Verleihtyp</td>
                <td></td>
            </tr>
          </table>
    </div>
</body>
</html>