All pastes #926292 Raw Edit

Someone

public text v1 · immutable
·published 2008-03-03 17:23 UTC
rendered paste body
#!/usr/bin/php -q
<?php

# nao deixar esse script rodar por mais do que 60 segundos
set_time_limit(60);

# desabilita a saida do buffer
ob_implicit_flush(false);

# desabilita as mensagens de erro, 
error_reporting(0);

# difinicao das variaveis de controle (stdin,stout,stderr)
if (!defined('STDIN'))
{
   define('STDIN', fopen('php://stdin', 'r'));
}
if (!defined('STDOUT'))
{
   define('STDOUT', fopen('php://stdout', 'w'));
}
if (!defined('STDERR'))
{
   define('STDERR', fopen('php://stderr', 'w'));
}

# define o arquivo de log
if (!defined('STDLOG'))
{
        define('STDLOG', fopen("/var/log/asterisk/my_agi.log", "a"));
}

# habilita ou nao o debug
if (!defined('DEBUG'))
{
        define ('DEBUG', FALSE);
}

# habilita ou nao o log 
if (!defined('SAVELOG'))
{
        define ('SAVELOG', FALSE);
}


# funcao para escrever mensagens 
# no console do asterisk (DEBUG)
#
# parametros 
#       string $STR_MESSAGE     : String a ser impressa na tela
#
#       exemplo : write_console("Discando para SIP/1234");
#

function write_console($STR_MESSAGE){
        fwrite(STDOUT,"verbose \"$STR_MESSAGE\" \n");
}


# funcao para executar comando  (EXEC)
#
# parametros 
#       string $STR_CMD         : Comando a ser executado
#       string $STR_PARAM       : Parametros do comando
#
#       exemplo : exec_command("DIAL","SIP/12345");
#
function exec_command($STR_CMD, $STR_PARAM){
        $COMMAND = "$STR_CMD \"$STR_PARAM\" \n";

        fwrite(STDOUT,"$COMMAND");
        fflush(STDOUT);
}

function checkresult($res) 
        { 
                trim($res); 
                if (preg_match('/^200/',$res)) 
                { 
                        if (! preg_match('/result=(-?\d+)/',$res,$matches)) 
                        { 
                                fwrite(STDERR,"FAIL ($res)\n"); 
                                fflush(STDERR); 
                                return 0; 
                        } 
                        else 
                        { 
                                fwrite(STDERR,"PASS (".$matches[1].")\n"); 
                                fflush(STDERR); 
                                return $matches[1]; 
                        } 
                } 
                else 
                { 
                        fwrite(STDERR,"FAIL (unexpected result '$res')\n"); 
                        fflush(STDERR); 
                        return -1; 
                } 
        } 

# captura todas as variaveis AGI vindas do asterisk 
# e salva na variavel $AGI
while (!feof(STDIN))
{
    $TEMP = trim(fgets(STDIN,4096));
    if (($TEMP == "") || ($TEMP == "\n"))
    {
        break;
    }
    $SPLIT = split(":",$TEMP);
    $NAME = str_replace("agi_","",$SPLIT[0]);
    $AGI[$NAME] = trim($SPLIT[1]);
}

# Escre na tela todas as variaveis do AGI
# para proposito de DEBUG

if (DEBUG) {
        write_console("Asterisk AGI Variables");
        write_console("==================================================");

        foreach($AGI as $KEY=>$VALUE)
        {
                write_console("-- agi_$KEY = $VALUE");
        }
}

# Executa a funcao exec_command 
exec_command("GET VARIABLE","UNIQUEID");


$UNIQUEID = fgets(STDIN, 4096);
fwrite(STDLOG,$UNIQUEID. "\n");
$UNIQUEID = trim($UNIQUEID);


# Executa a funcao exec_command
exec_command("GET VARIABLE","SIPCALLID");
$SIPCALLID = fgets(STDIN, 4096);
fwrite(STDLOG,$SIPCALLID, "\n");
$SIPCALLID = trim($SIPCALLID);


write_console("=======Variavel UNIQUEID vale:  $UNIQUEID===========");
write_console("=======Variavel SIPCALLID vale:  $SIPCALLID===========");
?>