System Command not executing php

Home » Asterisk Users » System Command not executing php
Asterisk Users 5 Comments

Try put the php in the AGI directory and call it via AGI() in Asterisk.

Jian

On 11-08-24 10:21 AM, Tim King wrote:
> *I have been testing this for a week now, and I am still struggling to
> make it work. Here is the output from extension 11 just to show that
> permissions are correct and asterisk can access faxnotify.php*
>
> — Executing [11@outb2:1] Verbose(“SIP/616818xxxx-000002f6”,
> “Testing….”) in new stack
> Testing….
> — Executing [11@outb2:2] Set(“SIP/616818xxxx-000002f6”,
> “FNAME=/var/lib/asterisk/bin/faxnotify.php”) in new stack
> — Executing [11@outb2:3] Verbose(“SIP/616818xxxx-000002f6”,
> “Size: 2553”) in new stack
> Size: 2553
> — Executing [11@outb2:4] Verbose(“SIP/616818xxxx-000002f6”,
> “Mode: 100777”) in new stack
> Mode: 100777
> — Executing [11@outb2:5] Verbose(“SIP/616818xxxx-000002f6”,
> “#!/usr/bin/php
> < ?php
>
> // set some global variables here
> $SUCCESS_STATUS = “FAX_SUCCESS”;
> $FROM_EMA”) in new stack
> #!/usr/bin/php
> < ?php
>
> // set some global variables here
> $SUCCESS_STATUS = FAX_SUCCESS;
> $FROM_EMA
> — Executing [11@outb2:6] TrySystem(“SIP/616818xxxx-000002f6”,
> “/var/lib/asterisk/bin/faxnotify.php”) in new stack
> — Executing [11@outb2:7] Verbose(“SIP/616818xxxx-000002f6”,
> “SUCCESS”) in new stack
> SUCCESS
> — Executing [11@outb2:8] Hangup(“SIP/616818xxxx-000002f6”, “”) in
> new stack
> == Spawn extension (outb2, 11, 8) exited non-zero on
> ‘SIP/616818xxxx-000002f6’
>
> If I execute from the command line as user asterisk the script works
> correctly. If i execute extension 777 test.php is called and works
> correctly. The only thing I wonder is if asterisk is somehow not
> correctly passing the variables to faxnotify.php causing it to fail.
> Can someone please test this configuration on their system and let me
> know if they see what I am doing wrong?
>
> This works as user asterisk.
> */var/lib/asterisk/bin/faxnotify.php NOTIFY “tim.compnetwork@gmail.com
> ” “6165550000” “24/08/11 : 09:00:00”
> “FAX_SUCCESS” “1”*
> *
> Here are the permissions of the folder.
> *drwxr-xr-x 2 asterisk asterisk 4.0K Aug 24 13:19 .
> drwxr-xr-x 13 asterisk asterisk 4.0K Aug 22 12:38 ..
> -rwxrwxrwx 1 asterisk asterisk 2.5K Aug 24 11:43 faxnotify.php
> -rwxr-xr-x 1 asterisk asterisk 6.4K Aug 20 10:26 fax-process.pl
> http://fax-process.pl
> -rwxrwxrwx 1 asterisk asterisk 73 Aug 24 12:52 test.php
> -rwxrwxrwx 1 asterisk asterisk 60 Aug 24 11:40 test.sh
> *
> [outbound]*
> exten => 11,1,Verbose(Testing….)
> exten => 11,n,Set(FNAME=/var/lib/asterisk/bin/faxnotify.php)
> exten => 11,n,Verbose(Size: ${STAT(s,${FNAME})})
> exten => 11,n,Verbose(Mode: ${STAT(m,${FNAME})})
> exten => 11,n,Verbose(${FILE(${FNAME},,100)})
> exten => 11,n,TrySystem(${FNAME})
> exten => 11,n,Verbose(${SYSTEMSTATUS})
> exten => 11,n,Hangup()
>
> exten => 777,1,System(/var/lib/asterisk/bin/test.php)
> exten => 777,n,Playback(vm-goodbye)
> exten => 777,n,Hangup()
>
> *[ext-fax]*
> exten => s,1,Noop(Receiving Fax for: ${FAX_RX_EMAIL} , From:
> ${CALLERID(all)})
> exten => s,n(receivefax),StopPlaytones
> exten => s,n,ReceiveFAX(${ASTSPOOLDIR}/fax/${UNIQUEID}.tif,f)
> exten => s,n,ExecIf($[“${FAXOPT(error)}”=””]?Set(FAXSTATUS=FAILED
> LICENSE EXCEEDED))
> exten => s,n,ExecIf($[“${FAXOPT(error)}”!=”” &&
> “${FAXOPT(error)}”!=”NO_ERROR”]?Set(FAXSTATUS=”FAILED FAXOPT: error:
> ${FAXOPT(error)} status: ${FAXOPT(status)} statusstr: ${FAX
> exten => s,n,Hangup
> exten => h,1,GotoIf($[“${FAXSTATUS:0:6}” = “FAILED”]?failed)
> exten => h,n(process),GotoIf($[${LEN(${FAX_RX_EMAIL})} = 0]?end)
> exten => h,n,System(${ASTVARLIBDIR}/bin/fax-process.pl
> http://fax-process.pl –to “${FAX_RX_EMAIL}” –from
> “noreply@mydomain.com ” –dest
> “${FROM_DID}” –subject “New fax from ${URIENCODE(${CALLERI
> exten => h,n(end),Macro(hangupcall,)
> exten => h,process+101(failed),Noop(FAX ${FAXSTATUS} for:
> ${FAX_RX_EMAIL} , From: ${CALLERID(all)})
> exten => h,n,Macro(hangupcall,)
>
> *[app-fax]*
> include => app-fax-custom
> exten => 6169804602,1,Set(FAX_RX_EMAIL=tim.compnetwork@gmail.com
> )
> exten => 6169804602,n,Goto(ext-fax,s,1)
> exten => h,1,Macro(hangupcall,)
>
>
> *test.php*
>
> < ?php
> exec(“touch /var/lib/asterisk/bin/testfile.txt”);
>
> *faxnotify.php*
>
> #!/usr/bin/php
> < ?php
>
> // set some global variables here
> $SUCCESS_STATUS = “FAX_SUCCESS”;
> $FROM_EMAIL = “fax@mydomain.com “;
> $FROM_NAME = “mydomain Fax”;
> $CONTACT_EMAIL = “support@mydomain.com “;
>
> if ($argc != 7) {
> echo “Usage faxnotify.php messagetype email destination timestamp
> status_string pages n “;
> echo “message type can be one of INIT or NOTIFY “;
> }
> echo “Starting faxnotify.php n”;
>
>
> // setting up
> $messtype = $argv[1];
> $email = $argv[2];
> $dest = $argv[3];
> $timestamp = $argv[4];
> $status = $argv[5];
> $numpages = $argv[6];
>
> $headers = “From: $FROM_NAME < $FROM_EMAIL>“;
>
> // end setting up
>
> if ($messtype == “INIT”) { // SendFAX called successfully in the
> dialplan…
> $emailSubject = “Your fax to $dest has been initiated”;
> $notice = “Your fax to $dest sent on $timestamp has been
> initiated. You will get a notification ” .
> “email when the transmission is complete. “;
> $emailBody = “Hi! nn” . $notice . ” nn ” .
> “If you have any queries, please
> contact us at: $CONTACT_EMAIL”;
> mail($email, $emailSubject, $emailBody, $headers);
> }
> else { // meaning $messtype = “NOTIFY” … sending of fax is
> complete. Need to check if SUCCEEDED
> $tech_details = “—————————— n”.
> “DESTINATION = $dest n”.
> “TIMESTAMP =
> $timestamp n”.
> “FAXOPTS_STATUSSTRING =
> $status n”.
> “NUM_PAGES =
> $numpages n”.
>
> “—————————— n”;
>
>
> echo “Sending fax notification email to: $email from
> $FROM_EMAIL n”;
>
> if($status == $SUCCESS_STATUS) {
> $emailSubject = “Your fax to $dest was delivered successfully”;
> $notice = “This is an automated response to let you know
> that your fax to ” .
> “$dest sent on $timestamp was
> delivered successfully. n”;
> } else {
> $emailSubject = “Your fax to $dest could not be sent”;
> $notice = “This is an automated response to let you know
> that your fax to ” .
> “$dest sent on $timestamp could not be
> delivered. n”;
> }
>
> $emailBody = “Hi! nn” . $notice . “nn” . $tech_details .
> ” nn ” .
> “If you beleive there was an error
> please contact: $CONTACT_EMAIL”;
>
> // echo $emailSubject . “n”;
> // echo $emailBody . “n”;
>
> // mail
> mail($email, $emailSubject, $emailBody, $headers );
> // exec(“echo $email $timestamp $emailSubject >>
> /var/log/asterisk/webfax.log”);
> // exec(“echo $emailBody >> /var/log/asterisk/webfax.log”);
> // exec(“echo ——————————– >>
> /var/log/asterisk/webfax.log”);
> }
> ?>
>
>
>
>
> —
> _____________________________________________________________________
> — Bandwidth and Colocation Provided by http://www.api-digital.com
> New to Asterisk? Join us for a live introductory webinar every Thurs:
> http://www.asterisk.org/hello
>
> asterisk-users mailing list
> To UNSUBSCRIBE or update options visit:
> http://lists.digium.com/mailman/listinfo/asterisk-users

5 thoughts on - System Command not executing php

  • Try:
    exten => 777,1,AGI(faxnotify.php,NOTIFY, “tim.compnetwork@gmail.com
    ” ,”6165550000″, “24/08/11 :
    09:00:00″, “FAX_SUCCESS”, “1”)

  • Un-top-posting…

    If the script does not conform to the AGI protocol, do not call it with
    agi().

  • I admit that using AGI() may not correct here. This is a work around
    about passing multiple args to php script. (I had this trouble before.)
    I would like to know what is the correct way to call the php script
    through SYSTEM().

    Jian

  • (Please keep in mind I’m not a PHP weenie so my experiences may be colored
    by my C background.)

    Depending on how your host is set up and the contents of your script, it
    may be as simple as:

    exten = *,n,system(example.php)

    This assumes your script starts with a ‘shebang’ like:

    #!/usr/bin/php -f

    and that the path to example.php is enumerated in the PATH environment
    variable of the Asterisk process.

    If these conditions are not met, you can specify your way around them in
    the parameters (arguments sound so argumentative) passed to the system()
    application.

    The process executing Asterisk must also have execute permission to every
    directory in the path to the script and both read and execute permissions
    to the script. Using ‘777’ is insecure and the mark of a newb.

    You can pass multiple parameters on the command line like:

    exten = *,n,system(echo ${EPOCH} ${CHANNEL} >/tmp/foo)

    You can confirm its execution with:

    cat /tmp/foo

    (When I wrote this example, I used the EXTEN channel variable instead of
    EPOCH. Extra points for the first reader to explain why that was not a
    good example. Anyone, anyone?)

    So the problem is not passing multiple parameters, but accessing them in
    your program.

    In most programming languages, the program initialization code creates and
    populates an array containing either the parameters or pointers to the
    parameters.

    In PHP, the array is named $argv. In C, it is whatever the programmer
    named the second parameter to the main() function. I think most C
    programmers name this array of char pointers argv because everybody
    expects that it have this name.

    In any case, the ‘zeroth’ element contains the path to the program which
    may be useful if you have multiple links to the program because you want
    the program to behave differently depending on which link was used to
    execute the program. Probably not, but now you know.

    Within your script you can access the command line parameters with code
    like:

    #!/usr/bin/php -f
    < ? echo "The zeroth parameter is "$argv[0]"n";
    echo “The first parameter is “$argv[1]”n”;
    echo “The second parameter is “$argv[2]”n”;

    exit(0);

    ?>

    If you were to execute this script with a dialplan like:

    exten = *,n,system(echo.php ${EPOCH} ${CHANNEL} >/tmp/foo)

    you would find that /tmp/foo contained:

    The zeroth parameter is “./echo.php”
    The first parameter is “1314224887”
    The second parameter is “SIP/841-line-1-b7961340”

    Personally (leaping up on my soapbox), I find specifying a bunch of
    anonymous parameters difficult to maintain. I prefer to use the getopt()
    function in PHP (or getopt_long() function in C) and define a
    ‘self-documenting’ command line. (I also do this for my AGIs.)

    For example, I would define ‘–channel’ and ‘–epoch’ options so that I
    could execute the above example like:

    exten = *,n,system(echo.php –epoch=${EPOCH} –channel=${CHANNEL})

    or

    exten = *,n,system(echo.php –channel=${CHANNEL} –epoch=${EPOCH})

    Aside from allowing you to specify the parameters in any order (I’m big on
    ‘alphabetization’), it is much easier to maintain when you pass several
    parameters.

  • Thank you Steve for the reply. I tested your way and it works. So I am
    going to correct my scripts. (But I hasn’t figure out your question
    about the using of EXTEN channel variable. I can only guessing it could
    be you are using “*”.)

    Cheers,

    jian