<?
  $host
="localhost";
  
$port=5038;
  
$user="";
  
$pass="";
  
$context="ivr"// the context your redirects are inside of
  
$priority="1";  // the default priority for redirects
//End of variables section.
 
function astconnect($action$command$host$port$user$pass){
  
$socket fsockopen("$host","$port");
  
fputs($socket"Action: Login\r\n");
  
fputs($socket"UserName: $user\r\n");
  
fputs($socket"Secret: $pass\r\n\r\n");
  
fputs($socket"Action: $action\r\n");
  if(isset(
$command)) { fputs($socket"Command: $command\r\n\r\n"); }
  
fputs($socket"Action: Logoff\r\n\r\n");
    while (!
feof($socket)) {
     
$wrets .= fread($socket8192);
    }
     
fclose($socket);
     return 
$wrets;
 }
 
 function 
sec2hms ($sec$padHours false) {
   
$hms "";
   
$hours intval(intval($sec) / 3600);
   
$hms .= ($padHours)
         ? 
str_pad($hours2"0"STR_PAD_LEFT). ':'
         
$hours':';
   
$minutes intval(($sec 60) % 60);
   
$hms .= str_pad($minutes2"0"STR_PAD_LEFT). ':';
   
$seconds intval($sec 60);
   
$hms .= str_pad($seconds2"0"STR_PAD_LEFT);
   return 
$hms;
 }

function 
is_number($number)
{
    
$text = (string)$number;
    
$textlen strlen($text);
    if (
$textlen==0) return 0;
    for (
$i=0;$i $textlen;$i++)
    { 
$ch ord($text{$i});
       if ((
$ch<48) || ($ch>57)) return 0;
    }
    return 
1;
}
  
$querystring $_SERVER['QUERY_STRING'];
  
$cleanup astconnect("Status""""$host""$port""$user""$pass");

  
// this part parses $cleanup to keep a pseudo channel from stealing the next extesion and time
  
$wrets str_replace("Zap/pseudo""Zap/pseudo\nExtension: Psuedo Channel\nSeconds: Psuedo Channel\n"$cleanup);  

  
// these next lines parse $wrets and grep for the relavent information placing it in an array.
  
$exp explode("\n"$wrets);
   foreach (
preg_grep('/CallerIDName:/'$exp) as $manifest){
    
$explode ereg_replace("\r"""str_replace("CallerIDName: """$manifest));
    
$cidname_array[]=$explode;
  }
  
$exp2 explode("\n"$wrets);
   foreach (
preg_grep('/CallerID:/'$exp2) as $manifest){
    
$explode ereg_replace("\r"""str_replace("CallerID: """$manifest));
     
$npa[]="".$explode[0]."".$explode[1]."".$explode[2]."";
     
$nxx[]="".$explode[3]."".$explode[4]."".$explode[5]."";
     
$cidnum_array[]=$explode;
  }
  
$exp3 explode("\n"$wrets);
   foreach (
preg_grep('/Channel:/'$exp3) as $manifest){
    
$explode ereg_replace("\r"""str_replace("Channel: """$manifest));
    
$channel_array[]=$explode;
  }
  
$exp4 explode("\n"$wrets);
   foreach (
preg_grep('/State:/'$exp4) as $manifest){
    
$explode ereg_replace("\r"""str_replace("State: """$manifest));
    
$state_array[]=$explode;
  }
  
$exp5 explode("\n"$wrets);
   foreach (
preg_grep('/Seconds:/'$exp5) as $manifest){
    
$explode ereg_replace("\r"""str_replace("Seconds: """$manifest));
    
$seconds_array[]=$explode;
  }
  
$exp6 explode("\n"$wrets);
   foreach (
preg_grep('/Extension:/'$exp6) as $manifest){
    
$explode ereg_replace("\r"""str_replace("Extension: """$manifest));
    
$extension_array[]=$explode;
  }

  echo(
"
  <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
    \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" >
   <head>
    <title>ASWIP Script - V0.1</title>
    <meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\" />
    <meta name=\"author\" content=\"Raymond Fannon\" />
    <meta name=\"keywords\" content=\"Asterisk, PHP, Manager, Ray Fannon, Telephreak, Gid\" />
    <meta name=\"description\" content=\"A modular PHP based Asterisk user manager.\" />
    <meta name=\"robots\" content=\"all\" />
    <style type=\"text/css\" media=\"all\">
     @import \"style.css\";
    </style>
   </head>
   <body>
    <div id=\"container\">
   <form name=\"nav\" action=\""
$_SERVER['PHP_SELF'] ."\" method=\"post\">
   <table id=\"main\">
    <tr>
     <th colspan=\"2\">Caller ID Number</th><th>Caller ID Name</th><th>Extension</th><th>Time</th>
    </tr>
  "
);
  
$i 0;
  if (
count($state_array) == "0") {
   echo(
"<tr class=\"$row_color\"><th colspan=\"6\">Sorry. No results.</th></tr>");
  } else {
   while(
$i count($state_array)) {
    
$row_color = ($i 2) ? tr1 tr2;
    echo(
"
     <tr class=\"$row_color\">
      <th><input type=\"radio\" name=\"selection\" value=\""
.htmlspecialchars($channel_array[$i])."\" /></th>
      <td>
     "
);
 
$cidnumlength strlen($cidnum_array[$i]);
 if(
is_number($cidnum_array[$i])) {
   if(
$cidnumlength == "10") {
    echo(
"<a href=\""$_SERVER['PHP_SELF'] ."?lnpa=".$npa[$i]."&lnxx=".$nxx[$i]."\">".htmlspecialchars($cidnum_array[$i])."</a>");
   } else if(
$cidnumlength == "11") {
  
$cidnum $cidnum_array[$i];
  
$npa $cidnum[1].$cidnum[2].$cidnum[3];
  
$nxx $cidnum[4].$cidnum[5].$cidnum[6];
   echo(
"<a href=\""$_SERVER['PHP_SELF'] ."?lnpa=".$npa."&lnxx=".$nxx."\">".htmlspecialchars($cidnum_array[$i])."</a>");
   }
   else {
    echo 
htmlspecialchars($cidnum_array[$i]);
  }
}   else {
    echo 
htmlspecialchars($cidnum_array[$i]);
      }

echo(
"</td><td>".htmlspecialchars($cidname_array[$i])."</td><td>".$extension_array[$i]."</td><td>".sec2hms($seconds_array[$i])."</td>
     </tr>
   "
);
   
$i++;
   }
  }
 echo(

    <tr>
     <td colspan=\"6\" class=\"tdhead\">
     <select name=\"action\" onchange=\"this.form.submit()\">
      <option value=\"null\">Action:</option>
      <option value=\"hangup\">Hangup</option>
      <option value=\"blacklist\">Blacklist</option>
     </select>
     <select name=\"transfer\" onchange=\"this.form.submit()\">
      <option value=\"null\">Transfer:</option>
      <option value=\"2\">Conference (2)</option>
      <option value=\"2600\">2600 Conf (2600)</option>
      <option value=\"187\">187 Conf</option> 
      <option value=\"69\">Telephuck (69)</option>
      <option value=\"187\">Whatever 187</option>
      <option value=\"31415\">Pi (31415)</option>
      <option value=\"9333\">Gonads & Strife (9333)</option>
      <option value=\"226262\">Banana Phone (226262)</option>
      <option value=\"462\">Forty Six & 2 (462)</option>
      <option value=\"7422\">RIAA (7422)</option>
      <option value=\"2442536\">Chinese Chicken (2442536)</option>
     </select>
     <select name=\"geninfo\" onchange=\"this.form.submit()\">
      <option value=\"null\">General Info:</option>
      <option value=\"showchannel\">Channel Information</option>
      <option value=\"netstats\">Show Netstats (IAX2)</option>
      <option value=\"sipshowregistry\">Show Registry (SIP)</option>
      <option value=\"iax2showregistry\">Show Registry (IAX2)</option>
      <option value=\"showcodecs\">Show Codecs</option>
     </select>
    </form>
    </td>
   </tr>
   <tr class=\"tr1\">
    <td colspan=\"6\"> <a href=\""
.$_SERVER['PHP_SELF']."\">Active Calls</a> - <a href=\"".$_SERVER['PHP_SELF']."?blacklist=edit\">Edit Blacklist</a> - <a href=\"".$_SERVER['PHP_SELF']."?bridgecall\">Bridge in a call</a></td>
   </tr>
   <tr class=\"tr2\">
    <td colspan=\"6\">
     <br />
"
);
if(isset(
$_GET['info'])) {                                          
     
$command astconnect("Command""show channel ".$_GET['info']."""$host""$port""$user""$pass");
     echo(
"<pre>".$command."</pre>");
  }

 if(isset(
$_GET['bridgesubmit'])) {
  
$myFile "/var/spool/asterisk/outgoing/bridge.call";
  
$fh fopen($myFile'w') or die("can't open file");
  
$stringData "Channel:  SIP/".$_POST['phonenumber']."@voipbuster\nCallerID: Bridged Call <".$_POST['phonenumber'].">\nMaxRetries: 1\nRetryTime: 30\nWaitTime: 30\nContext: conferences\nExtension: bridge\nPriority: 1\n";
  
fwrite($fh$stringData);
   
fclose($fh);
    if(
$fh) { echo"Attempting to bridge: ".$_POST['phonenumber'].".<br />"; }
    else    { echo
"BOOH!"; }
  }
if(isset(
$_GET['bridgecall'])) {
 echo(
"
 Enter the number you wish to call:
  <form method=\"post\" name=\"wargames\" action=\"?bridgesubmit\">
   <table>
    <tr> 
     <th colspan=\"2\">Example: 17025551212</th>
    </tr>
    <tr>
     <th>Number:</th>
     <th><input name=\"phonenumber\" id=\"phonenumber\" /></th>
    </tr>
    <tr>
     <th colspan=\"2\"><input type=\"submit\" value=\"Submit\" /><input type=\"reset\" value=\"Reset\" /></th>
    </tr>
   </table>
  </form>
<p />
 "
);
}
if(
$_GET['blacklist'] == "add") {
          
$socket fsockopen("$host","$port");
           
fputs($socket"Action: Login\r\n");
           
fputs($socket"UserName: $user\r\n");
           
fputs($socket"Secret: $pass\r\n\r\n");
           
fputs($socket"Action: Command\r\n");
           
fputs($socket"Command: database put blacklist ".$_POST['blckaddcid']." 1\r\n\r\n");
           
fputs($socket"Action: Logoff\r\n\r\n");
             while (!
feof($socket)) {
             
$wrets .= fread($socket8192);
             }
           
fclose($socket);
    echo(
"The number ".$_POST['blckaddcid']." has been added to the blacklist.");
}
if(
$_GET['blacklist'] == "delete") {
          
$socket fsockopen("$host","$port");
           
fputs($socket"Action: Login\r\n");
           
fputs($socket"UserName: $user\r\n");
           
fputs($socket"Secret: $pass\r\n\r\n");
           
fputs($socket"Action: Command\r\n");
           
fputs($socket"Command: database del blacklist ".$_GET['cidnum']."\r\n\r\n");
           
fputs($socket"Action: Logoff\r\n\r\n");
             while (!
feof($socket)) {
             
$wrets .= fread($socket8192);
             }
           
fclose($socket);
    echo(
"The number ".$_GET['cidnum']." has been removed from the blacklist.");
}
if(isset(
$_GET['blacklist'])) {
          
$socket fsockopen("$host","$port");
           
fputs($socket"Action: Login\r\n");
           
fputs($socket"UserName: $user\r\n");
           
fputs($socket"Secret: $pass\r\n\r\n");
           
fputs($socket"Action: Command\r\n");
           
fputs($socket"Command: database show blacklist\r\n\r\n");
           
fputs($socket"Action: Logoff\r\n\r\n");
             while (!
feof($socket)) {
             
$wrets .= fread($socket8192);
             }
           
fclose($socket);
          
$explode explode("\n"$wrets);
    echo(
"<table width=\"100%\"><tr><td valign=\"top\" width=\"50%\">");
         echo(
"<table width=\"100%\" border=\"1\">");
     echo(
"<tr><th>Number</th><th>Options</th></tr>");
            foreach (
preg_grep('/blacklist/'$explode) as $manifest)
             {
               
$wspace eregi_replace("[[:space:]]+"" "$manifest);
        
$number1 str_replace('/'' '$wspace);
              
$number explode(" "$number1);
               
$cidnpa $number['2'];
                  
$npa "".$cidnpa['0']."".$cidnpa['1']."".$cidnpa['2']."";
                  
$nxx "".$cidnpa['3']."".$cidnpa['4']."".$cidnpa['5']."";
        echo(
"<tr><td><a target=\"_self\" href=\"https://www.telephreak.org/npa/lookup.php?npa=".$npa."&nxx=".$nxx."\"><font color=\"#000000\">"$number['2'] ."</a></td><td><a target=\"_self\" href=\""$_SERVER['PHP_SELF'] ."?blacklist=delete&cidnum="$number['2'] ."\"> Delete </td></tr>\n");
         }
        echo(
"</table>\n");
    echo(
"</td><td valign=\"top\" width=\"50%\">");
        echo(
"<table border=\"1\" width=\"100%\"><tr><th>Blacklist Number Add</th></tr><tr><td>");
    echo(
"<form id=\"blacklistadd\" action=\"?blacklist=add\" method=\"post\">\n");
    echo(
"<input type=\"text\" id=\"blckcid\" name=\"blckaddcid\" />\n");
    echo(
"<input type=\"submit\" value=\"Add Number\" />\n");
    echo(
"</tr></tr></table>\n");

    echo(
"</td></tr></table>");
        echo(
"<a href=\""$_SERVER['PHP_SELF'] ."\"> Back </a>");
}
if(isset(
$_GET['lnpa']) && isset($_GET['lnxx'])) {
echo(
"<iframe frameborder=\"0\" name=\"frame\" width=\"550\" height=\"125\" src=\"http://www.telephreak.org/npa/lookup.php?npa=".$_GET['lnpa']."&nxx=".$_GET['lnxx']."\" >
Use a browser that handles frames, please.</iframe>"
);
}

if(
$_POST['geninfo'] == "null" && isset($_POST['selection'])) {
 if(
$_POST['action'] != "null") {
  if(
$_POST['action'] == "hangup") {
   
$command astconnect("Command""soft hangup ".$_POST['selection']."""$host""$port""$user""$pass");   
    echo(
"
       Channel: "
.$_POST['selection']." has been hung up.<br />
        <a href=\""
.$_SERVER['PHP_SELF']."\">Refresh this page</a>
    "
);
  }
  if(
$_POST['action'] == "blacklist") {
   
$command explode("\n"astconnect("Command""show channel ".$_POST['selection']."""$host""$port""$user""$pass"));
    
$cidnumorig explode(" "$command[10]);
    
$cidnum $cidnumorig[8];
    
$blacklistmojo astconnect("Command""database put blacklist $cidnum 1".$_POST['selection']."""$host""$port""$user""$pass");
    echo(
"The number $cidnum has been added to the blacklist.");
          
$socket fsockopen("$host","$port");
           
fputs($socket"Action: Login\r\n");
           
fputs($socket"UserName: $user\r\n");
           
fputs($socket"Secret: $pass\r\n\r\n");
           
fputs($socket"Action: Command\r\n");
           
fputs($socket"Command: database show blacklist\r\n\r\n");
           
fputs($socket"Action: Logoff\r\n\r\n");
             while (!
feof($socket)) {
             
$wrets .= fread($socket8192);
             }
           
fclose($socket);
          
$explode explode("\n"$wrets);
        echo(
"<table width=\"100%\" border=\"1\">\n");
        echo(
"<tr><td align=\"center\" valign=\"top\" width=\"50%\">");
               echo(
"<table width=\"100%\" border=\"1\">");
               echo(
"<tr><td>Number</td><td>Options</td></tr>");
            foreach (
preg_grep('/blacklist/'$explode) as $manifest)
             {
               
$wspace eregi_replace("[[:space:]]+"" "$manifest);
                
$number1 str_replace('/'' '$wspace);
                  
$number explode(" "$number1);
               
$cidnpa $number['2'];
                  
$npa "".$cidnpa['0']."".$cidnpa['1']."".$cidnpa['2']."";
                  
$nxx "".$cidnpa['3']."".$cidnpa['4']."".$cidnpa['5']."";
                echo(
"<tr><td><a target=\"_self\" href=\"https://www.telephreak.org/npa/lookup.php?npa=".$npa."&nxx=".$nxx."\"><font color=\"#000000\">"$number['2'] ."</a></td><td><a target=\"_self\" href=\""$_SERVER['PHP_SELF'] ."?blacklist=delete&cidnum="$number['2'] ."\"> Delete </td></tr>\n");
             }
                echo(
"</table>\n");
        echo(
"</td><td align=\"center\" valign=\"top\" width=\"50%\">\n");
        echo(
"<form id=\"blacklistadd\" action=\"?blacklist=add\" method=\"post\">\n");
        echo(
"<input type=\"text\" id=\"blckcid\" name=\"blckaddcid\" />\n");
        echo(
"<input type=\"submit\" value=\"blckadd\" />\n");
        echo(
"</td></tr>\n</table><br />\n");
        echo(
"<a href=\""$_SERVER['PHP_SELF'] ."\"> Back </a>");

}
}
  if(
$_POST['transfer'] != "null") {
  
$socket fsockopen("$host","$port");
  
fputs($socket"Action: Login\r\n");
  
fputs($socket"UserName: $user\r\n");
  
fputs($socket"Secret: $pass\r\n\r\n");
  
fputs($socket"Action: Redirect\r\n");
  
fputs($socket"Channel: ".$_POST['selection']."\r\n");
  
fputs($socket"Exten: ".$_POST['transfer']."\r\n");
  
fputs($socket"Context: ".$context."\r\n");
  
fputs($socket"Priority: ".$priority."\r\n");
  
fputs($socket"Action: Logoff\r\n\r\n");
  
fclose($socket);
   echo(
"The user has been transferred.");
 }
}
/*
if(!isset($_POST['selection']) && $_POST['geninfo'] == "null") {
 echo("Please select a channel. <p />");
}
*/
if(isset($_POST['geninfo']) && $_POST['geninfo'] != "null") {
 if(
$_POST['geninfo'] == "showchannel") {
  
$command astconnect("Command""show channel ".$_POST['selection']."""$host""$port""$user""$pass");
    echo(
"<pre>".$command."</pre>");
 }
 if(
$_POST['geninfo'] == "netstats") {
  
$command astconnect("Command""iax2 show netstats""$host""$port""$user""$pass");
    echo(
"<pre>".$command."</pre>");
 }
 if(
$_POST['geninfo'] == "sipshowregistry") {
  
$command astconnect("Command""sip show registry""$host""$port""$user""$pass");
    echo(
"<pre>".$command."</pre>");
 }
 if(
$_POST['geninfo'] == "iax2showregistry") {
  
$command astconnect("Command""iax2 show registry""$host""$port""$user""$pass");
    echo(
"<pre>".$command."</pre>");
 }
 if(
$_POST['geninfo'] == "showcodecs") {
  
$command astconnect("Command""core show codecs""$host""$port""$user""$pass");
    echo(
"<pre>".$command."</pre>");
 }
}
/*
//    this area is for debugging

echo("<p /><hr /><pre>");
print_r($_POST);
echo("</pre><p />");
echo("<p /><hr /><p /><pre>".$wrets."</pre><br />");
*/
echo("
    </td>
   </tr>
  </table>
   </div>
   </body>
  </html>
 "
);
?>