<?php

function pdir($code, $ua) {
    $url = 'http://www.ciscodirectory.com/dir/'.urlencode($code);
    $options = array(
        'http' => array(
            'user_agent'  => "$ua\r\n",
        )
    );
    $context  = stream_context_create($options);
    $fp = fopen($url, "rb", false, $context);
    fflush($fp);
    $resultb = stream_get_contents($fp);
    $result="";
    $meta = stream_get_meta_data($fp);
    foreach ($meta["wrapper_data"] as $value) {
        $result.=$value."\r\n";
    }
    $result.="\r\n".$resultb;
    return ($result);
}

$uidErr = "";

$uid=$_GET["uid"];
if (strpos($uid, '/') !== false || $uid=="." || $uid==".." ) {
    $uidErr = "Invalid characters";
}
$vendor = $_GET["vendor"];
$model = $_GET["model"];
$fw = $_GET["fw"];
$mac = $_GET["mac"];
$sn = $_GET["sn"];

?>
<!DOCTYPE HTML>  
<html>
<head>
<style>
.example {color: #C0C0C0; font-style: italic;}
.uid {color: #A0A050;}
.ua {color: #2000A0; font-style: bold;}
.error {color: #FF0000;}
</style>
</head>
<body> 
<h2>Cisco SPA client simulator</h2>
<form method="get" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
  UID: <input type="text" name="uid" value="<?php echo $uid;?>">
  <?php if (!empty($uidErr)) { echo "<span class=\"error\">$uidErr</span>"; }; ?>
  <span class="example">xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx</span>
  <br><br>
  Vendor: <input type="text" name="vendor" value="<?php echo $vendor;?>">
  <span class="example">Cisco</span>
  <br><br>
  Model: <input type="text" name="model" value="<?php echo $model;?>">
  <span class="example">SPA504G</span>
  <br><br>
  Firmware: <input type="text" name="fw" value="<?php echo $fw;?>">
  <span class="example">7.6.2b</span>
  <br><br>
  MAC: <input type="text" name="mac" value="<?php echo $mac;?>">
  <span class="example">000000000000</span>
  <br><br>
  Serial: <input type="text" name="sn" value="<?php echo $sn;?>">
  <span class="example">xxx00000xxx</span>
  <br><br>
  <input type="submit" name="submit" value="Submit">  
</form><hr />

<?php

if (!empty($_GET["uid"]) && empty($uidErr)) {

$ua = "$vendor/$model-$fw ($mac)($sn)";

trigger_error("PDIR for $ua @ $uid", E_USER_NOTICE);

$result = pdir($uid, $ua);

echo ("<h2>Result for <span class=\"ua\">$ua</span> @ <span class=\"uid\">$uid</span></h2>\r\n");
echo(str_replace("\r\n", "<br />\r\n", htmlentities($result, ENT_NOQUOTES | ENT_DISALLOWED | ENT_XHTML, "UTF-8")));

}
?></body></html>