AGI + PHP “Making phones jump through fiery hoops of death.” Rob Peck dealnews.com PHP Appalachia 2008.

Slides:



Advertisements
Similar presentations
1 Copyright © 2002 Pearson Education, Inc.. 2 Chapter 1 Introduction to Perl and CGI.
Advertisements

UFCE8V-20-3 Information Systems Development 3 (SHAPE HK) Lecture 3 PHP (2) : Functions, User Defined Functions & Environment Variables.
CGI & HTML forms CGI Common Gateway Interface  A web server is only a pipe between user-agents  and content – it does not generate content.
Adding Dynamic Content to your Web Site
Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
Common Gateway Interface (CGI). CGI is a protocol: CGI is not a programming language CGI is a protocol for the exchange of information between between.
Browsers and Servers CGI Processing Model ( Common Gateway Interface ) © Norman White, 2013.
PHP (2) – Functions, Arrays, Databases, and sessions.
Linux+ Guide to Linux Certification, Second Edition
Definitions, Definitions, Definitions Lead to Understanding.
How Clients and Servers Work Together. Objectives Learn about the interaction of clients and servers Explore the features and functions of Web servers.
Guide To UNIX Using Linux Third Edition
Real-time multimedia and communication in packet networks Asterisk The open source IP PBX.
Chapter 6: Hostile Code Guide to Computer Network Security.
CGI Programming Languages Web Based Software Development July 21, 2005 Song, JaeHa.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Autodial System BY: KATPALLY AMRUSHA ALIGAPALLY ANUSHA.
INTRODUCTION TO WEB DATABASE PROGRAMMING
Asterisk The Open Source PBX. What we will discuss... ● Functionality of a PBX... ● What is Asterisk... ● Setting up your own PBX...  Hardware needed.
NETWORK CENTRIC COMPUTING (With included EMBEDDED SYSTEMS)
Intermediate PHP (2) File Input/Output & User Defined Functions.
1 Chapter 6 – Creating Web Forms and Validating User Input spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
Internet Server Group zAndrew Kozarik - CGI zBrandon Zarzoza- C# zChris Young- Perl zLawrence M. - Python zJohn Hall - JSP zRay Skoog- TCL zChad Stadig-
Chapter 33 CGI Technology for Dynamic Web Documents There are two alternative forms of retrieving web documents. Instead of retrieving static HTML documents,
Asterisk & ENUM Extending the Open Source PBX Michael Haberler, IPA Otmar Lendl, nic.at.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
A gentle introduction to Asterisk Anthony Critelli.
VoIP, Asterisk, and Java Michael P. Plezbert Agilis Systems, Inc St. Louis Java Users Group April 13, 2006.
GDT V5 Web Services. GDT V5 Web Services Doug Evans and Detlef Lexut GDT 2008 International User Conference August 10 – 13  Lake Las Vegas, Nevada GDT.
Week 7 Working with the BASH Shell. Objectives  Redirect the input and output of a command  Identify and manipulate common shell environment variables.
An Introduction to Unix Shell Scripting
CS4273: Distributed System Technologies and Programming I Lecture 7: Java Networking.
Follow the Data Data (and information) move from place to place in computer systems and networks. As it moves it changes form frequently. This story describes.
Java CGI Lecture notes by Theodoros Anagnostopoulos.
10/13/2015 ©2006 Scott Miller, University of Victoria 1 Content Serving Static vs. Dynamic Content Web Servers Server Flow Control Rev. 2.0.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Data and Computer Communications Circuit Switching and Packet Switching.
1 Week 2 The Crunchy Shell to the Soft and Chewy Kernel… Sarah Diesburg 8/3/2010 COP4610 / CGS5765.
Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.
Website Development with PHP and MySQL Saving Data.
1 Welcome to CSC 301 Web Programming Charles Frank.
Real-time multimedia and communication in packet networks Asterisk AGI and Manager Interface.
Quiz 15 minutes Open note, open book, open computer Finding the answer – working to get it – is what helps you learn I don’t care how you find the answer,
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
Real-time multimedia and communication in packet networks iLanga.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Creating Web Documents: How the Web works Client / Server Protocols Access methods Homework: Complete experiment & report on Discussion Forum.
Form Data Encoding GET – URL encoded POST – URL encoded
SKYPIAX, how to add Skype capabilities to FreeSWITCH (and Asterisk) CHICAGO, USA, September 2009.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
1) PHP – Personal Home Page Scripting Language 2) JavaScript.
Web Programming Overview. Introduction HTML is limited - it cannot manipulate data How Web pages are extended (include): –Java: an object-oriented programming.
Introduction to CGI PROG. CGI stands for Common Gateway Interface. CGI is a standard programming interface to Web servers that gives us a way to make.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Linux Administration Working with the BASH Shell.
Lesson 11. CGI CGI is the interface between a Web page or browser and a Web server that is running a certain program/script. The CGI (Common Gateway Interface)
The Common Gateway Interface (CGI) Pat Morin COMP2405.
1 Chapter 1 INTRODUCTION TO WEB. 2 Objectives In this chapter, you will: Become familiar with the architecture of the World Wide Web Learn about communication.
By Asma Hamad Alharbi.
CS 330 Class 7 Comments on Exam Programming plan for today:
Business and Technical details Mark Spencer
VoIP with asterisk © Mathias Weyland.
DBW - PHP DBW2017.
Introduction to Programming the WWW I
What is Bash Shell Scripting?
Presentation transcript:

AGI + PHP “Making phones jump through fiery hoops of death.” Rob Peck dealnews.com PHP Appalachia 2008

Introduction to VoIP At its simplest, sending telephone calls over an IP network. No magic. “It’s just software.” Voice packets are treated just like any other packet on a network. Broken down, transmitted, and reassembled.

Do It Open Source! Asterisk is an open-source (GPL) software PBX (Private Branch Exchange) that was created by Mark Spencer. Basic Asterisk software includes many features available in proprietary PBX systems. Runs on ordinary PC hardware.

How Asterisk Works extensions.conf is the config file that controls how Asterisk handles calls. It is called the “dialplan.” Commands are sequentially executed based on the status from the previous command. exten => _1XXX,1,Answer() exten => _1XXX,n,Wait(2)

I Need Something Else! Asterisk can be extended to do almost anything using AGI, or the Asterisk Gateway Interface. You can write scripts for AGI in any language (Perl, Python, C, etc). My favorite, of course, is PHP.

Anatomy of an AGI call 1. Dialplan calls AGI 2. AGI reads variables from Asterisk 3. AGI sends commands to Asterisk 4. AGI reads response from Asterisk 5. Repeat 3-4 as necessary. 6. ??? 7. Profit! 8. AGI exits

How does AGI work? AGI reads data from and writes data to Asterisk, much the same way CGI would do with a web server. When Asterisk starts an AGI script, it feeds the channel variables to the script on standard input...

Reading From Asterisk Every time an AGI script executes, Asterisk passes a number (about 20) values to the script. These AGI headers take the form of "key: value", one per line separated with a line feed, concluding with a blank line.

Look! Some Code! 1. ob_implicit_flush(true); 2. set_time_limit(6); 3. $in = fopen("php://stdin","r"); 4. $stdlog = fopen("php://stderr", "w"); while ($env=read()) { 7. $s = split(": ",$env); 8. $key = str_replace("agi_","",$s[0]); 9. $value = trim($s[1]); 10. $_AGI[$key] = $value; 11. if (($env == "") || ($env == "\n")) { 12. break; 13. } 14. } function read() { 17. global $in, $debug, $stdlog; 18. $input = str_replace("\n", "", fgets($in, 4096)); 19. return $input; 20. }

What can I do with it? You now have data from the server to interact with. This may include caller ID, channel, extension, etc, in an $_AGI array. With this you can interact with databases, write logs, route calls. Pretty much anything you can think of.

Interacting Writing data back to the channel. Sending commands to stdout in the form of AGI commands. There are around 30 native AGI commands, and hundreds of apps. Commands return data in the form: 200 result=x

AGI Commands ANSWER - Answers a channel GET DATA - Gets digits SAY * - Reads data back to the channel. SET * - Sets various options on a channel. VERBOSE - Sends data to the CLI. EXEC - Executes a dialplan app.

Commands vs. Apps Applications (“apps”) are functions that can be run in the Asterisk dialplan itself, or in an AGI via EXEC. Commands are AGI specific actions that can only be executed from within an AGI.

Interacting 1. function write($line) { 2. global $debug, $stdlog; 3. echo $line."\n"; 4. } function execute($command) { 7. global $in, $out, $debug, $stdlog; 8. write($command); 9. $data = fgets($in, 4096); 10. if (preg_match("/^([0-9]{1,3}) (.*)/", $data, $matches)) { 11. if (preg_match(’/^result=([0-9a-zA-Z]*)( ?\((.*)\))?$/’, $matches[2], $match)) { 12. $arr['code'] = $matches[1]; 13. $arr['result'] = $match[1]; 14. if (isset($match[3]) && $match[3]) { 15. $arr['data'] = $match[3]; 16. } 17. return $arr; 18. } else return 0; 19. } else return -1; 20. }

Simple AGI 1. #!/usr/bin/php 2. <?php 3. include "agi.php"; execute("SAY DATETIME #"); ?>

More Complex 1. #!/usr/bin/php 2. <?php include "agi.php" ; $db=mysql_connect(’redacted’, ‘redacted’, ‘redacted’); if(!$db) { 9. verbose("Could not connect to DB!"); 10. exit(1); 11. } if(!mysql_select_db(’redacted’, $db)) { 14. verbose("Could not use DB!"); 15. exit(1); 16. } $res=mysql_query(sprintf("select substitution_name from cid_substitution where from_number=’%s’",mysql_escape_string($_AGI['callerid']))); $result=mysql_fetch_row($res); if(!empty($result)) { 23. execute(sprintf("SET CALLERID \"%s \"",$result[0],$_AGI['callerid'])); 24. } mysql_close($db); ?>

“Calling” your AGI 1. exten => 1000,1,Wait(2) 2. exten => 1000,n,AGI(test.php)

AGI Execution AGI() - simple AGI execution. EAGI() - provides access to the audio channel. FastAGI() - Allows AGI execution on remote server(s). DeadAGI() - Allows AGI access to a “dead” (hungup) channel.

Passing Variables Two ways to pass AGI variables to the script: channel variables and command- line arguments. Channel variables set from the dialplan. exten => 1000,n,Set(VAR=1) Read it from within the script: $var = execute(“get variable VAR”);

Passing Variables Command line variables called from the AGI command. exten => 1000,n,AGI(test.php|test) These are available via $argv just like a shell script.

Passing Variables AGI scripts can pass variables back to Asterisk for use in the dialplan. This is accomplished by calling the SET VARIABLE command to set the variable on the channel. Variables can then be read in dialplan applications.

Common Problems AGI scripts live in /var/lib/asterisk/agi- bin PHP script must be executable by the user Asterisk is running as (usually “asterisk”).

Debugging your AGI No way around it, debugging an AGI is a pain. One shortcut is to use AGI’s “verbose” command to output text to the Asterisk CLI. You can then connect to the Asterisk CLI and watch calls proceed. Softphones are a lifesaver.

AGI Debugging “AGI DEBUG” within the CLI will show what is sent and received from each AGI command.

Beyond AGI Asterisk Manager Interface (AMI) allows you to interact with Asterisk outside of a call. Callfiles allow you to easily originate a call by dropping a specially formatted file into a directory.

Summary AGI allows you to extend Asterisk using any language of your choice. AGI scripts may read and write only once, or many times. Read from stdin, write to stdout. Allows you to construct complicated actions that interact with outside data.

Slides, Notes, Etc. Slides (will be on my blog): Asterisk: Lots of info (“the wiki”):