<?php
ini_set('max_execution_time', 99999);
function getext($ip)
	{
	$ch = curl_init("http://$ip/cgi-bin/sip");
//curl_setopt ($ch, CURLOPT_COOKIEJAR, COOKIE_FILE); 
	//echo($ch);
	curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
//curl_setopt($ch, CURLOPT_HEADER, true);
	$remote=curl_exec ($ch);
	curl_close($ch);
	//echo($remote);
	$ext="---";
	if ($remote!=false)
		{
		$t1=explode("\" name=user_number size=16 maxlength=31>", $remote);
		$t2=explode("<input value=\"", $t1[0]);
		$ext=$t2[5];
		}
	return $ext;
	}
	
function firstplug($ip)
	{
	//echo($ip);
	$url = "http://$ip/cgi-bin/login_handle";
$fields = array(
	'auth' => urlencode(""),
	'login' => urlencode("  Enter  ")
);
$fields_string="";
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);
if (strpos($result, "Password Incorrect!")!==false) $nuevo=false;
else $nuevo=true;

//close connection
curl_close($ch);
return $nuevo;
	}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin t&iacute;tulo</title>
</head>

<body>
<table width="1003" border="1" cellspacing="2" cellpadding="3">
  <tr>
    <td width="87" bgcolor="#CCCCCC"><div align="center">IP</div></td>
    <td width="110" bgcolor="#CCCCCC"><div align="center">MAC</div></td>
    <td width="131" bgcolor="#CCCCCC"><div align="center">TITULO</div></td>
    <td width="68" bgcolor="#CCCCCC"><div align="center">NUEVO</div></td>
    <td width="95" bgcolor="#CCCCCC"><div align="center">EXTENSION</div></td>
    <td width="132">&nbsp;</td>
    <td width="157">&nbsp;</td>
    <td width="139">&nbsp;</td>
  </tr>
<?php
$ipinicio=$_POST["ipinicio"];
$ipfin=$_POST["ipfin"];
$ipi=explode(".", $ipinicio);
$ipf=explode(".", $ipfin);
for ($a=$ipi[0]; $a<=$ipf[0]; $a++)
	for ($ab=$ipi[1]; $ab<=$ipf[1]; $ab++)
			for ($abc=$ipi[2]; $abc<=$ipf[2]; $abc++)
					for ($abcd=$ipi[3]; $abcd<=$ipf[3]; $abcd++)
						{
						//echo("$a.$ab.$abc.$abcd<br>");
						$ch = curl_init("http://$a.$ab.$abc.$abcd");
						//curl_setopt ($ch, CURLOPT_COOKIEJAR, COOKIE_FILE); 
						curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
						curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
						//curl_setopt($ch, CURLOPT_HEADER, true);
						$remote=curl_exec ($ch);
						curl_close($ch);
						if ($remote!=false)
							{
							$t1=@explode("<title>", $remote);
							$t2=@explode("</title>", $t1[1]);
							$titulo=$t2[0];	
							//echo($titulo);
							ob_flush();
							$ip="$a.$ab.$abc.$abcd";			
						?>
  <tr>
    <td><a href="http://<?php echo("$a.$ab.$abc.$abcd"); ?>" target="<?php echo("$a.$ab.$abc.$abcd"); ?>"><?php echo("$a.$ab.$abc.$abcd"); ?></a></td>
    <td>&nbsp;</td>
    <td><?= $titulo ?></td>
    <td><?php if ($titulo=="Bittel IPPhone") 
	$new=firstplug("$a.$ab.$abc.$abcd");
	if($new) echo("Si");
	else echo("No"); ?></td>
    <td><?php if ($titulo=="Bittel IPPhone") $ext=getext("$a.$ab.$abc.$abcd");
	echo($ext); ?></td>
    <td><a href="fwupg.php?ip=<?= $ip ?>" target="UPD">ACT. FIRMWARE</a></td>
    <td><form action="confext.php?ip=<?= $ip ?>" method="post" name="form1" target="EXT" id="form1">
      CONF EXT:
      <input name="ext" type="text" id="ext" size="5" maxlength="5" />      
          <input type="submit" name="button" id="button" value="&gt;" />
    </form>
    </td>
    <td>&nbsp;</td>
  </tr>
<?php
							}
						}
?>
</table>
</body>
</html>
