#!/usr/bin/perl
 
 use IO::Socket::INET ;
 
 my $EOL = "\015\012";
 my $BLANK = $EOL x 2;
 
 my $AMPUSER = "admin";        # Voir Fichier /etc/asterisk/manager.conf
 my $AMPPASS = "amp111";
 my $CHANNEL = "SIP/1234";     # Votre extension SIP
 my $WAITTIME = "30";
 my $CALLERID = "WebCall <33101020304>";    # Votre numéro, visible par l'appélé et l'appelant
 
 
 sub call {
       my ($EXTEN) = @_;
       $EXTEN = '0'.$EXTEN;
 
       my $socket = IO::Socket::INET -> new (
       PeerAddr => '127.0.0.1' ,
       PeerPort => 5038 ,
       Proto => 'tcp' ,
       Type => SOCK_STREAM
       );
 
       $socket->autoflush(1);
 
       #$socket -> recv ( $message , 128 ) ;
       #print $message ;
 
       print $socket "Action: Login${EOL}Username: ${AMPUSER}${EOL}Secret: ${AMPPASS}$BLANK";
 
       print $socket "Action: originate${EOL}Channel: ${CHANNEL}${EOL}WaitTime: ${WAITTIME}${EOL}CallerId: ${CALLERID}${EOL}Exten: ${EXTEN}${EOL}Context: from-internal${EOL}Priority: 1$BLANK";
 
       print $socket "Action: Logoff$BLANK";
 
 }
 
 my (%postdata,$data,$pair);
 $data='';
 if ($ENV{'REQUEST_METHOD'} eq 'POST') {
       my $len=$ENV{'CONTENT_LENGTH'};
       if (read(STDIN,$data,$len) != $len) {print ("<H1>error reading post data </H1>"); die("Error reading 'POST' data\n"); }
       }
       elsif ($ENV{'REQUEST_METHOD'} eq 'GET') {
               $data=$ENV{'QUERY_STRING'};
       }
 
       foreach $pair (split('&',$data)) {
               my ($name,$value)=split('=',$pair);
               $name=~ tr/\0//d;
               $name =~ tr/+/ /;
               $name =~ s/%([0-9a-fA-F]{2})/chr hex($1)/ge;
               $postbrut{$name}=$value;
               $value=~ tr/\0//d;
               $value=~ tr/+/ /;
               $value=~ s/%([0-9a-fA-F]{2})/chr hex($1)/ge;
               $postdata{$name}=$value;
               }
 
 print "Content-type: text/html\n\n";
 
 if ($postdata{txtphonenumber} eq '') {
 print <<EOF
 <p>
 <table width="300" border="1" bordercolor="#630000" cellpadding="3" cellspacing="0">
       <tr><td>
       <font size="2" face="verdana,arial,georgia" color="#630000">Enter your phone number in international format (33XXXXXXXXX), and we will call you a few seconds later!</font>
       <form action="" method="post">
       <input type="text" size="20" maxlength="20" name="txtphonenumber"><br>
       <input type="submit" value="Call Us!">
       </form>
       </td></tr>
 </table>
 </p>
 EOF
 } else {
 call($postdata{txtphonenumber});
 print <<EOF
 <p>
 <table width="300" border="1" bordercolor="#630000" cellpadding="3" cellspacing="0">
       <tr><td>
       <font size="2" face="verdana,georgia" color="#630000">We are currently trying to call you.  Please be patient, and wait for your phone to ring!<br>If your phone does not ring after 2 minutes, we apologize, but must either be out, or already on the phone.<br><a href="">Try Again</a></font>
       </td></tr>
 </table>
 </p>
 EOF
 }