TIPS Getting a PHP script to execute an Asterisk CLI command

Phoenix

New Member
Joined
May 25, 2013
Messages
17
Reaction score
2
I am trying to build a simple web-interface so that users who do not have access to a SIP client or will be calling from the move (where data is often sketchy) can enter their phone-number and the number they want to call...

My inspiration came from here http://www.kammerath.net/click-to-call.html (in German) where he uses a "shell exec" command to run a CLI command.

Having now built a little php script doing the same thing, but it does not seem to run commands - no calls are getting placed and I cannot find any indication in the logs that the script even tried to run a command.

I placed the script in the /var/www/html directory on the piaf box. I'll of course need to find a good way to protect access to it for full production use...
Is there anything missing in the stock PIAF box to run shell exec commands? Or could somebody point out what I am missing?

Here's the script:
Code:
<?php
$result = "";
if(array_key_exists("num1",$_GET) == true
        && array_key_exists("num2",$_GET) == true){
 
        $num1 = $_GET["num1"];
        $num2 = $_GET["num2"];
 
        /* check if both numbers start with a +*/
        if(substr($num1,0,1)=="+" && substr($num2,0,1)=="+"){
                $num1 = substr($num1,1);
                $num2 = substr($num2,1);
                if(is_numeric($num1)==true && is_numeric($num2)){
                        shell_exec("sudo asterisk -rx \"originate Local/"
                                .$num1."@outbound-allroutes "."extension ".$num2."@outbound-allroutes\"");
 
                        $result = "call initiated";
                }else{
                        $result = "The number is invalid. It needs to start with "
                                ."a + and then only contain numbers (e.g. +493030303030)";
                }
        }else{
                $result = "The number is invalid. It needs to start with a + (e.g. +49).";
        }
}
?>
<html>
<head>
<title>Click-to-Call Simple PHP Example</title>
</head>
<body>
<?php
if($result != ""){
        echo "Status: ".$result;
}
?>
<form method="get">
        Source number:<br />
        <input type="text" name="num1" /><br />
        <br />
        Destination number:<br />
        <input type="text" name="num2" /><br />
        <br />
        <input type="submit" value="Initiate call" />
</form>
</body>
</html>
 

tm1000

Schmoozecom INC/FreePBX
Joined
Dec 1, 2009
Messages
1,360
Reaction score
78
PHP:
<?php
if (!@include_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) {
      include_once('/etc/asterisk/freepbx.conf');
}
if($astman->connected()) {
    $out = $astman->Command('sip show registry');
    echo $out['data'];
} else {
    echo "not asterisk manager connection";
}
 

Phoenix

New Member
Joined
May 25, 2013
Messages
17
Reaction score
2
Thank you for your replies. And yes, lgaetz, you interpreted correctly that I want to execute it straight from the webpage as tm1000 showed.

I have been quite busy lately but will try the method out asap.
 

Members online

Forum statistics

Threads
25,810
Messages
167,754
Members
19,240
Latest member
nikko
Get 3CX - Absolutely Free!

Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.
Top