Koozali.org: home of the SME Server

Asterisk 1.8 Call Pop Up

Offline apmuthu

  • *
  • 244
  • +0/-0
Asterisk 1.8 Call Pop Up
« on: May 02, 2013, 11:26:48 PM »
Asterisk v1.8 Call Pop Up using pure PHP:
astman.php
Code: [Select]

 <?php 
class AstMan {
  var 
$socket;
  var 
$error;
  
  function 
AstMan()
  {
    
$this->socket FALSE;
    
$this->error "";
  } 
  function 
Login($host="localhost"$username="admin"$password="amp111"){
    
    
$this->socket = @fsockopen($host,"5038"$errno$errstr1); 
    if (!
$this->socket) {
      
$this->error =  "Could not connect - $errstr ($errno)";
      return 
FALSE;
    }else{
      
stream_set_timeout($this->socket1); 
  
      
$wrets $this->Query("Action: Login\r\nUserName: $username\r\nSecret: $password\r\nEvents: off\r\n\r\n"); 
      if (
strpos($wrets"Message: Authentication accepted") != FALSE){
        return 
true;
      }else{
      
$this->error "Could not login - Authentication failed";
        
fclose($this->socket); 
        
$this->socket FALSE;
      return 
FALSE;
      }
    }
  }
  
  function 
Logout(){
    if (
$this->socket){
      
fputs($this->socket"Action: Logoff\r\n\r\n"); 
      while (!
feof($this->socket)) { 
        
$wrets .= fread($this->socket8192); 
      } 
      
fclose($this->socket); 
      
$this->socket "FALSE";
    }
   return; 
  }
  
  function 
Query($query){
    
$wrets "";
    
    if (
$this->socket === FALSE)
      return 
FALSE;
      
    
fputs($this->socket$query); 
    do
    {
      
$line fgets($this->socket4096);
      
$wrets .= $line;
      
$info stream_get_meta_data($this->socket);
    }while (
$line != "\r\n" && $info['timed_out'] == false );
    return 
$wrets;
  }
  
  function 
GetError(){
    return 
$this->error;
  }
  
  function 
GetDB($family$key){
    
$value "";
  
    
$wrets $this->Query("Action: Command\r\nCommand: database get $family $key\r\n\r\n");
  
    if (
$wrets){
      
$value_start strpos($wrets"Value: ") + 7;
      
$value_stop strpos($wrets"\n"$value_start);
     if (
$value_start 8){
        
$value substr($wrets$value_start$value_stop $value_start);
      }
   }
    return 
$value;
  } 
  
  function 
PutDB($family$key$value){
    
$wrets $this->Query("Action: Command\r\nCommand: database put $family $key $value\r\n\r\n");
  
   if (
strpos($wrets"Updated database successfully") != FALSE){
    return 
TRUE;
    }
    
$this->error =  "Could not updated database";
    return 
FALSE;
  } 
  
  function 
DelDB($family$key){
    
$wrets $this->Query("Action: Command\r\nCommand: database del $family $key\r\n\r\n");
   if (
strpos($wrets"Database entry removed.") != FALSE){
    return 
TRUE;
    }
    
$this->error =  "Database entry does not exist";
    return 
FALSE;
  } 
  
  
  function 
GetFamilyDB($family){
    
$wrets $this->Query("Action: Command\r\nCommand: database show $family\r\n\r\n");
    if (
$wrets){
      
$value_start strpos($wrets"Response: Follows\r\n") + 19;
      
$value_stop strpos($wrets"--END COMMAND--\r\n"$value_start);
     if (
$value_start 18){
        
$wrets substr($wrets$value_start$value_stop $value_start);
      }
      
$lines explode("\n"$wrets);
      foreach(
$lines as $line){
        if (
strlen($line) > 4){
          
$value_start strpos($line": ") + 2;
          
$value_stop strpos($line" "$value_start);
         
$key trim(substr($linestrlen($family) + 2strpos($line" ") - strlen($family) + 2));   
          
$value[$key] = trim(substr($line$value_start));
        }
      }
      return 
$value;
   }
    return 
FALSE;
  }   
}

parse_ast.php
Code: [Select]
<?php
// Author: Ap.Muthu apmuthu@usa.net
// Sponsor: PlaNettel.com.sg
// Release Date: 2013-05-03
 
// Constants
// $astserver = '127.0.0.1'; // for use on asterisk server
$astserver '192.168.1.10';
$astuser 'admin';
$astpass 'secret';
/*
$extn = '201'; // come from parameter settings
$pcip = '192.168.1.102'; // come from parameter settings
*/
if ($argc <> 3) exit;
$extn trim(' ' $argv[1]);
$pcip trim(' ' $argv[2]);

// login to asterisk manager interface
include ("astman.php");
$astman = new AstMan();
//$astman->Login("192.168.20.10", "admin", "planettel");
$astman->Login($astserver$astuser$astpass);
// extract the Caller ID if present and call number
// if call number is old exit
$callerid '';
$rcvextn '';
 
//get the caller id name already set for this call using the PHP Asterisk Manager
 //first get a list of the active channels and return the first one that has a caller id value set.
 
$value $astman->Query("Action: Command\r\nCommand: core show channels concise\r\n\r\n");
 
$chan_array preg_split("/\n/",$value);
 
 foreach(
$chan_array as $val)
 {
  
$this_chan_array explode("!",$val);
  if(isset(
$this_chan_array[7]))
  {
    
$value $astman->Query("Action: Command\r\nCommand: core show channel $this_chan_array[0]\r\n\r\n");
    
$this_array preg_split("/\n/",$value);
    foreach(
$this_array as $val2)
    {
     if(
strpos($val2,'Caller ID Name: ') !== false)
     {
      
$callerid trim(str_replace('Caller ID Name: ','',$val2));
     }
     if(
strpos($val2,'Connected Line ID: ') !== false)
     {
      
$rcvextn trim(str_replace('Connected Line ID: ','',$val2));
      if (
$rcvextn == $extn) break;
      else {
$rcvextn ''$callerid ''; }
     }
     
    }

    
//break out if the value is set.
    
if($callerid != '')
    {
     break;
    }
  }
 }
// build url with caller id
$url "http://myurl.com/myfile.php?CallerID=$callerid&AgentExtn=$extn&AgentPCIP=$pcip";
get_headers($url);


Usage from the client pc inside the php folder:
Code: [Select]

 php parse_ast.php 201 192.168.1.102
Either a cron job in linux or directly from the dialplan in the asterisk server is also possible.
 

Offline amorphys

  • 1
  • +0/-0
Re: Asterisk 1.8 Call Pop Up
« Reply #1 on: April 15, 2016, 09:31:59 AM »
Nice work, but for some reason not working for me :(

SMP Debian 3.16.7-ckt11-1 (2015-05-24) i686 GNU/Linux
Asterisk 1.8.32.3

Apache/2.4.10 (Debian)
PHP Version 5.6.9-0+deb8u1
Mysql    5.5.43


I don`t know how to run this part :(


// build url with caller id
$url = "http://myurl.com/myfile.php?CallerID=$callerid&AgentExtn=$extn&AgentPCIP=$pcip";
get_headers($url);



this :

php parse_ast.php 201 192.168.1.102

is the extensions number and client pc ? ip ?

So what i was hope to achieve was to have a webpage open on a client pc when a specific call was inbound in asterisk server with some info regarding the caller like callerid etc