Classe AGI - PHP 5.x
Contents Package AGI Procedural Elements 2 agi_lib_v5x.php 2 Package AGI Classes 3 Class AGI 3 Constructor construct 3 Method exec_command 4 Method getagi_env 4 Method getdebug 4 Method getsavelog 5 Method getstderr 5 Method getstdin 5 Method getstdlog 5 Method getstdout 6 Method get_result 6 Method init 6 Method setagi_env 7 Method setdebug 7 Method setsavelog 8 Method setstderr 8 Method setstdin 8 Method setstdlog 9 Method setstdout 9 Method write_console 10 Appendices 11 Appendix A - Class Trees 12 AGI 12 Appendix C - Source Code 13 Package AGI 14 source code: agi_lib_v5x.php 15
Page 1 of 22
Package AGI Procedural Elements agi_lib_v5x.php AGI Classe para trabalhar com AGI no Asterisk com PHP 5.x Package AGI Author Carlos Alberto Cesario < carloscesario@gmail.com> Since 09/03/2008 Filesource Source Code for this file License GPL Page 2 of 22
Package AGI Classes Class AGI [line 28] AGI Classe para trabalhar com AGI no Asterisk com PHP 5.x Package AGI Author Carlos Alberto Cesario < Version 0.2.3 Since 09/03/2008 License GPL carloscesario@gmail.com> Constructor void function AGI:: construct() [line 311] AGI:: construct() Metodo construtor da classe AGI Page 3 of 22
void function AGI::exec_command($STR_CMD, $STR_PARAM) [line 431] Function Parameters: string $STR_CMD Comando a ser executado string $STR_PARAM Parametros do comando AGI::exec_command() Funcao para executar comandos AGI 1 <?php 2 $AGI = new AGI(); 3 $AGI-> init(); 4 $AGI-> exec_command("exec DIAL","SIP/12345" ); 5?> array function AGI::getAgi_Env() [line 298] AGI::getAgi_Env() Recebe o valor da variavel AGI_ENV 1 <?php 2 $AGI = new AGI(); 3 $AGI-> init(); 4 $AGIENV = $AGI-> getagi_env(); 5 $EXTEN = $AGIENV['extension']; 6 echo $EXTEN; 7?> boolean function AGI::getDebug() [line 259] AGI::getDebug() Recebe o valor da variavel $DEBUG Page 4 of 22
boolean function AGI::getSavelog() [line 224] AGI::getSavelog() Recebe o valor da variavel $SAVE_LOG stream function AGI::getStderr() [line 169] AGI::getStderr() Recebe o valor da variavel $STDERR stream function AGI::getStdin() [line 108] AGI::getStdin() Recebe o valor da variavel $STDIN string function AGI::getStdlog() [line 197] AGI::getStdlog() Recebe o valor da variavel $STDLOG Page 5 of 22
stream function AGI::getStdout() [line 143] AGI::getStdout() Recebe o valor da variavel $STDOUT mixed function AGI::get_result() [line 468] AGI::get_result() Funcao para tratar o resultado da funcao exec_command() 1 <?php 2 $AGI = new AGI(); 3 $AGI-> init(); 4 $AGI-> exec_command("get VARIABLE", "UNIQUEID" ); 5 $UNIQUEID = $AGI-> get_result(); 6 $UNIQUEID = "code - ". $UNIQUEID['code']. 7 " :: result - ". $UNIQUEID['result']. 8 " :: data - ". $UNIQUEID['data']; 9 echo $UNIQUEID; 10?> void function AGI::init() [line 522] AGI::init() Inicializa a captura das variveis passadas pelo asterisk Page 6 of 22
void function AGI::setAgi_Env($AGIENV_VAR, $AGIENV_VALUE) [line 275] Function Parameters: string $AGIENV_VAR Nome da variavel do AGI string $AGIENV_VALUE Valor do AGI AGI::setAgi_Env() Define na variavel AGI_ENV todas as variaveis que o asterisk passa void function AGI::setDebug($DEBUG_VAR) [line 246] Function Parameters: boolean $DEBUG_VAR True ou False AGI::setDebug() Habilita ou nao o DEBUG dos comandos 1 <?php 2 $AGI = new AGI(); 3 $AGI-> setdebug(true); 4 $AGI-> init(); 5?> Page 7 of 22
void function AGI::setSavelog($STDLOG_VAR) [line 211] Function Parameters: boolean $STDLOG_VAR AGI::setSavelog() Habilita ou nao o LOG dos comandos stream function AGI::setStderr($STDERR_VAR) [line 156] Function Parameters: $STDERR_VAR AGI::setStderr() Define o stream para a variavel $STDERR void function AGI::setStdin($STDIN_VAR) [line 95] Function Parameters: stream $STDIN_VAR Stream stdin AGI::setStdin() Define o stream para a variavel $STDIN Page 8 of 22
void function AGI::setStdlog($STDLOG_VAR) [line 184] Function Parameters: string $STDLOG_VAR Caminho do arquivo de log AGI::setStdlog() Define o arquivo onde serao gravados os logs dos comandos void function AGI::setStdout($STDOUT_VAR) [line 130] Function Parameters: stream $STDOUT_VAR Stream stdout AGI::setStdout() Define o stream para a variavel $STDOUT 1 <?php 2 $AGI = new AGI(); 3 $AGI-> setstdout(fopen('php://stdout', 'w')); 4 $AGI-> init(); 5?> Page 9 of 22
void function AGI::write_console($STR_MESSAGE, [$VBL = 1]) [line 403] Function Parameters: string $STR_MESSAGE String a ser impressa na tela integer $VBL Valor para o verbose AGI::write_console() Funcao para escrever mensagens na cli usado para debug 1 <?php 2 $AGI = new AGI(); 3 $AGI-> init(); 4 $AGI-> write_console("discando para SIP/1234" ); 5?> Page 10 of 22
Appendices Page 11 of 22
Appendix A - Class Trees Package AGI AGI AGI Page 12 of 22
Appendix C - Source Code Page 13 of 22
Package AGI Page 14 of 22
File Source for agi_lib_v5x.php Documentation for this file is available at agi_lib_v5x.php 1 <?php 2 3 /** 4 * AGI 5 * 6 * Classe para trabalhar com AGI no Asterisk com PHP 5.x 7 * 8 * @since 09/03/2008 9 * @author Carlos Alberto Cesario <carloscesario@gmail.com> 10 * @package AGI 11 * @license http://www.gnu.org/copyleft/gpl.html GPL 12 * @filesource 13 */ 14 15 /** 16 * AGI 17 * 18 * Classe para trabalhar com AGI no Asterisk com PHP 5.x 19 * 20 * @since 09/03/2008 21 * @author Carlos Alberto Cesario <carloscesario@gmail.com> 22 * @access public 23 * @version 0.2.3 24 * @package AGI 25 * @license http://www.gnu.org/copyleft/gpl.html GPL 26 */ 27 28 class AGI 29 { 30 /** 31 * Stream de entrada de dados 32 * 33 * @var stream 34 * @access private 35 */ 36 private $STDIN = null; 37 38 /** 39 * Stream de saida de dados 40 * 41 * @var stream 42 * @access private 43 */ 44 private $STDOUT = null; 45 46 /** 47 * Stream de saida dos erros 48 * 49 * @var stream 50 * @access private 51 */ 52 private $STDERR = null; 53 54 /** 55 * Arquivo para gravacao de logs 56 * 57 * @var string 58 * @access private 59 */ 60 private $STDLOG = null; 61 62 /** 63 * Variavel para habilitar ou nao o log dos comandos 64 * 65 * @var boolean 66 * @access private 67 */ Page 15 of 22
68 private $SAVE_LOG = null; 69 70 /** 71 * Variavel para habilitar ou nao o debug dos comandos 72 * 73 * @var boolean 74 * @access private 75 */ 76 private $DEBUG = null; 77 78 /** 79 * Variavel para armazenar todas as variaveis passadas pelo asterisk 80 * 81 * @var array [] 82 * @access private 83 */ 84 private $AGI_ENV = null; 85 86 /** 87 * AGI::setStdin() 88 * 89 * Define o stream para a variavel $STDIN 90 * 91 * @param stream $STDIN_VAR Stream stdin 92 * @return void 93 * @access public 94 */ 95 public function setstdin($stdin_var) 96 { 97 $this-> STDIN = $STDIN_VAR; 98 } 99 100 /** 101 * AGI::getStdin() 102 * 103 * Recebe o valor da variavel $STDIN 104 * 105 * @return stream Stream stdin 106 * @access public 107 */ 108 public function getstdin() 109 { 110 return $this-> STDIN; 111 } 112 113 /** 114 * AGI::setStdout() 115 * 116 * Define o stream para a variavel $STDOUT 117 * 118 * <code> 119 * <?php 120 * $AGI = new AGI(); 121 * $AGI->setStdout(fopen('php://stdout', 'w')); 122 * $AGI->init(); 123 *?> 124 * </code> 125 * 126 * @param stream $STDOUT_VAR Stream stdout 127 * @access public 128 * @return void 129 */ 130 public function setstdout($stdout_var) 131 { 132 $this-> STDOUT = $STDOUT_VAR; 133 } 134 135 /** 136 * AGI::getStdout() 137 * 138 * Recebe o valor da variavel $STDOUT 139 * 140 * @return stream Stream stdout 141 * @access public 142 */ 143 public function getstdout() 144 { 145 return $this-> STDOUT; 146 } 147 Page 16 of 22
148 /** 149 * AGI::setStderr() 150 * 151 * Define o stream para a variavel $STDERR 152 * 153 * @return stream Stream stderr 154 * @access public 155 */ 156 public function setstderr($stderr_var) 157 { 158 $this-> STDERR = $STDERR_VAR; 159 } 160 161 /** 162 * AGI::getStderr() 163 * 164 * Recebe o valor da variavel $STDERR 165 * 166 * @return stream Stream stderr 167 * @access public 168 */ 169 public function getstderr() 170 { 171 return $this-> STDERR; 172 } 173 174 /** 175 * AGI::setStdlog() 176 * 177 * Define o arquivo onde serao 178 * gravados os logs dos comandos 179 * 180 * @param string $STDLOG_VAR Caminho do arquivo de log 181 * @return void 182 * @access public 183 */ 184 public function setstdlog($stdlog_var) 185 { 186 $this-> STDLOG = $STDLOG_VAR; 187 } 188 189 /** 190 * AGI::getStdlog() 191 * 192 * Recebe o valor da variavel $STDLOG 193 * 194 * @return string Caminho do arquivo de log 195 * @access public 196 */ 197 public function getstdlog() 198 { 199 return $this-> STDLOG; 200 } 201 202 /** 203 * AGI::setSavelog() 204 * 205 * Habilita ou nao o LOG dos comandos 206 * 207 * @param boolean $STDLOG_VAR 208 * @return void 209 * @access public 210 */ 211 public function setsavelog($stdlog_var) 212 { 213 $this-> SAVE_LOG = $STDLOG_VAR; 214 } 215 216 /** 217 * AGI::getSavelog() 218 * 219 * Recebe o valor da variavel $SAVE_LOG 220 * 221 * @return boolean True ou False 222 * @access public 223 */ 224 public function getsavelog() 225 { 226 return $this-> SAVE_LOG; 227 } Page 17 of 22
228 229 /** 230 * AGI::setDebug() 231 * 232 * Habilita ou nao o DEBUG dos comandos 233 * 234 * <code> 235 * <?php 236 * $AGI = new AGI(); 237 * $AGI->setDebug(true); 238 * $AGI->init(); 239 *?> 240 * </code> 241 * 242 * @param boolean $DEBUG_VAR True ou False 243 * @return void 244 * @access public 245 */ 246 public function setdebug($debug_var) 247 { 248 $this-> DEBUG = $DEBUG_VAR; 249 } 250 251 /** 252 * AGI::getDebug() 253 * 254 * Recebe o valor da variavel $DEBUG 255 * 256 * @return boolean True ou False 257 * @access public 258 */ 259 public function getdebug() 260 { 261 return $this-> DEBUG; 262 } 263 264 /** 265 * AGI::setAgi_Env() 266 * 267 * Define na variavel AGI_ENV todas 268 * as variaveis que o asterisk passa 269 * 270 * @param string $AGIENV_VAR Nome da variavel do AGI 271 * @param string $AGIENV_VALUE Valor do AGI 272 * @return void 273 * @access public 274 */ 275 public function setagi_env($agienv_var, $AGIENV_VALUE) 276 { 277 $this-> AGI_ENV[$AGIENV_VAR] = $AGIENV_VALUE; 278 } 279 280 /** 281 * AGI::getAgi_Env() 282 * 283 * Recebe o valor da variavel AGI_ENV 284 * 285 * <code> 286 * <?php 287 * $AGI = new AGI(); 288 * $AGI->init(); 289 * $AGIENV = $AGI->getAgi_Env(); 290 * $EXTEN = $AGIENV['extension']; 291 * echo $EXTEN; 292 *?> 293 * </code> 294 * 295 * @return array 296 * @access public 297 */ 298 public function getagi_env() 299 { 300 return $this-> AGI_ENV; 301 } 302 303 /** 304 * AGI:: construct() 305 * 306 * Metodo construtor da classe AGI 307 * Page 18 of 22
308 * @return void 309 * @access public 310 */ 311 public function construct() 312 { 313 $this-> set_env(); 314 } 315 316 /** 317 * AGI::set_env() 318 * 319 * Define opcoes do PHP, para execucao do php em modo cli 320 * 321 * @return void 322 * @access private 323 */ 324 private function set_env() 325 { 326 /** 327 * nao deixar esse script rodar por mais do que 60 segundos 328 */ 329 set_time_limit(60); 330 331 /** 332 * desabilita a saida do buffer 333 */ 334 ob_implicit_flush(false); 335 336 /** 337 * desabilita as mensagens de erro 338 */ 339 error_reporting(0); 340 } 341 342 /** 343 * AGI::define_handlers() 344 * 345 * Define valores padroes para os streams de controle, 346 * arquivo de log, debug. 347 * 348 * @return void 349 * @access private 350 */ 351 private function define_handlers () 352 { 353 if (!$this-> getdebug()) 354 { 355 $this-> setdebug(false); 356 } 357 358 if (!$this-> getsavelog()) 359 { 360 $this-> setsavelog(false); 361 } 362 363 if (!$this-> getstdin()) 364 { 365 $this-> setstdin(fopen('php://stdin', 'r')); 366 } 367 368 if (!$this-> getstdout()) 369 { 370 $this-> setstdout(fopen('php://stdout', 'w')); 371 } 372 373 if (!$this-> getstderr()) 374 { 375 $this-> setstderr(fopen ('php://stderr', 'w')); 376 } 377 378 if (!$this-> getstdlog()) 379 { 380 $this-> setstdlog(fopen('/var/log/asterisk/my_agi.log', 'a')); 381 } 382 } 383 384 /** 385 * AGI::write_console() 386 * 387 * Funcao para escrever mensagens na cli Page 19 of 22
388 * usado para debug 389 * 390 * <code> 391 * <?php 392 * $AGI = new AGI(); 393 * $AGI->init(); 394 * $AGI->write_console("Discando para SIP/1234"); 395 *?> 396 * </code> 397 * 398 * @param string $STR_MESSAGE String a ser impressa na tela 399 * @param integer $VBL Valor para o verbose 400 * @return void 401 * @access public 402 */ 403 public function write_console($str_message, $VBL = 1) 404 { 405 $STR_MESSAGE = str_replace("\\", "\\\\", $STR_MESSAGE); 406 $STR_MESSAGE = str_replace("\"", "\\\"", $STR_MESSAGE); 407 $STR_MESSAGE = str_replace("\n", "\\n", $STR_MESSAGE); 408 fwrite($this-> getstdout(), " VERBOSE \" $STR_MESSAGE\" $VBL\n" ); 409 fflush($this-> getstdout()); 410 fgets($this-> getstdin(), 1024); 411 } 412 413 /** 414 * AGI::exec_command() 415 * 416 * Funcao para executar comandos AGI 417 * 418 * <code> 419 * <?php 420 * $AGI = new AGI(); 421 * $AGI->init(); 422 * $AGI->exec_command("EXEC DIAL","SIP/12345"); 423 *?> 424 * </code> 425 * 426 * @param string $STR_CMD Comando a ser executado 427 * @param string $STR_PARAM Parametros do comando 428 * @return void 429 * @access public 430 */ 431 public function exec_command($str_cmd, $STR_PARAM) 432 { 433 $COMMAND = null; 434 if ($this-> getdebug()) 435 { 436 $this-> write_console(" --> cmd $STR_CMD $STR_PARAM" ); 437 } 438 if ($this-> getsavelog()) 439 { 440 fwrite($this-> getstdlog(), date("m d" ). " ". date("h:i:s" ). " -- agi -- COMANDO --> $STR_CMD $STR_PARAM \n" ); 441 } 442 $COMMAND = " $STR_CMD $STR_PARAM \n" ; 443 fwrite($this-> getstdout(), " $COMMAND" ); 444 fflush($this-> getstdout()); 445 } 446 447 /** 448 * AGI::get_result() 449 * 450 * Funcao para tratar o resultado da funcao exec_command() 451 * 452 * <code> 453 * <?php 454 * $AGI = new AGI(); 455 * $AGI->init(); 456 * $AGI->exec_command("GET VARIABLE", "UNIQUEID"); 457 * $UNIQUEID = $AGI->get_result(); 458 * $UNIQUEID = "code - ". $UNIQUEID['code']. 459 * " :: result - ". $UNIQUEID['result']. 460 * " :: data - ". $UNIQUEID['data']; 461 * echo $UNIQUEID; 462 *?> 463 * </code> 464 * 465 * @return mixed array 'code' 'result' 'data' or int 0-1 466 * @access public Page 20 of 22
467 */ 468 public function get_result() 469 { 470 $ARR = null; 471 $DATA = null; 472 $MATCHES = null; 473 $MATCH = null; 474 475 $DATA = fgets($this-> getstdin(), 4096); 476 if ($this-> getsavelog()) 477 { 478 fwrite($this-> getstdlog(), $DATA. "\n" ); 479 fwrite($this-> getstdlog(), "-------------------------\n" ); 480 } 481 482 /** 483 * Procura uma sequencia iniciada por 3 numeros de 0 a 9, 484 * seguidos ou nao de algum outro texto 485 */ 486 if (preg_match("/^([0-9]{1,3}) (.*)/", $DATA, $MATCHES)) 487 { 488 if (preg_match('/^result=([0-9a-za-z]*)(?\((.*)\))?$/', $MATCHES[2], $MATCH)) 489 { 490 $ARR['code'] = $MATCHES[1]; 491 $ARR['result'] = $MATCH[1]; 492 if (isset($match[3]) && $MATCH[3]) 493 { 494 $ARR['data'] = $MATCH[3]; 495 } 496 if ($this-> getdebug()) 497 { 498 $this- > write_console("==================================================" ); 499 $this-> write_console("ret CODE: ". $ARR['code']); 500 $this-> write_console("ret Value: ". $ARR['result']); 501 if ($ARR['data']) 502 { 503 $this-> write_console("ret DATA: ". $ARR['data']); 504 } 505 $this- > write_console("==================================================" ); 506 } 507 return $ARR; 508 } 509 else return 0; 510 } 511 else return -1; 512 } 513 514 /** 515 * AGI::init() 516 * 517 * Inicializa a captura das variveis passadas pelo asterisk 518 * 519 * @return void 520 * @access public 521 */ 522 public function init() 523 { 524 $this-> define_handlers(); 525 526 /** 527 * captura todas as variaveis AGI vindas do asterisk 528 * e salva na variavel $AGI do tipo array 529 */ 530 531 $TEMP = null; 532 $SPLIT = null; 533 $NAME = null; 534 535 while (!feof($this-> getstdin())) 536 { 537 $TEMP = trim(fgets($this-> getstdin(), 4096)); 538 if (($TEMP == "" ) ($TEMP == "\n" )) 539 { 540 break; 541 } 542 $SPLIT = split(":", $TEMP); 543 $NAME = str_replace("agi_", "", $SPLIT[0]); 544 $this-> setagi_env($name, trim($split[1])); Page 21 of 22
545 } 546 547 /** 548 * Escreve na tela todas as variaveis do AGI 549 * para proposito de DEBUG 550 */ 551 if ($this-> getdebug()) 552 { 553 $this-> write_console(" Asterisk A G I Variables " ); 554 $this-> write_console("==================================================" ); 555 556 foreach($this-> getagi_env() as $KEY => $VALUE) 557 { 558 $this-> write_console(" -- agi_$key = $VALUE" ); 559 } 560 561 $this-> write_console("==================================================" ); 562 } 563 } 564 } 565 566?> Page 22 of 22
Index A AGI::setStderr() 8 AGI::setStderr() AGI::setSavelog() 8 AGI::setSavelog() AGI::setDebug() 7 AGI::setDebug() AGI::setAgi_Env() 7 AGI::setAgi_Env() AGI::setStdin() 8 AGI::setStdin() AGI::setStdlog() 9 AGI::setStdlog() agi_lib_v5x.php 15 Source code AGI::write_console() 10 AGI::write_console() AGI::setStdout() 9 AGI::setStdout() AGI::init() 6 AGI::init() AGI::get_result() 6 AGI::get_result() AGI::getDebug() 4 AGI::getDebug() AGI::getAgi_Env() 4 AGI::getAgi_Env() AGI::exec_command() 4 AGI::exec_command() AGI 3 AGI AGI::getSavelog() 5 AGI::getSavelog() AGI::getStderr() 5 AGI::getStderr() AGI::getStdout() 6 AGI::getStdout() AGI::getStdlog() 5 AGI::getStdlog() AGI::getStdin() 5 AGI::getStdin() agi_lib_v5x.php 2 AGI
C constructor AGI:: construct() 3 AGI:: construct()