vTiger 5.1.0 Asterisk.1.4 integration update

Here the AsteriskClient.php script (/var/www/vtigercrm/cron/modules/PBXManager/) that works with Asterisk 1.4 and vTiger 5.1.0.
This scripts fixe the problem of a phone logged-in into a queue -> vTiger could
not pop-up the caller information.

This will fix it.

Cheers
Daniel

#!/usr/bin/php
/*+**********************************************************************************
* The contents of this file are subject to the vtiger CRM Public License Version 1.0
* (“License”); You may not use this file except in compliance with the License
* The Original Code is: vtiger CRM Open Source
* The Initial Developer of the Original Code is vtiger.
* Portions created by vtiger are Copyright (C) vtiger.
* All Rights Reserved.
************************************************************************************/

/**
* this file will be run as a shell script (in linux) or a batch file (under windows).
* the purpose of the file is to create a master socket which will be connecting to the asterisk server
* and to keep it (the socket) alive all the time.
*/

ini_set(“include_path”, “../../../”);
require_once(‘modules/PBXManager/utils/AsteriskClass.php’);
require_once(‘config.php’);
require_once(‘include/utils/utils.php’);
require_once(‘include/language/en_us.lang.php’);
require_once(‘modules/PBXManager/AsteriskUtils.php’);

$calls = array();
asteriskClient();

/**
* this function defines the asterisk client
*/
function asteriskClient(){
global $app_strings, $current_user;
global $adb, $log;

$data = getAsteriskInfo($adb);
$server = $data['server'];
$port = $data['port'];
$username = $data['username'];
$password = $data['password'];
$version = $data['version'];

$errno = $errstr = NULL;
$sock = @fsockopen($server, $port, $errno, $errstr, 3);
if( $sock === false ) {
$errno = $errstr = NULL;
$sock = @fsockopen($server, $port, $errno, $errstr, 1);
}
stream_set_blocking($sock, false);
if( $sock === false ) {
echo “Socket cannot be created due to error: $errno: $errstr\n”;
$log->debug(“Socket cannot be created due to error: $errno: $errstr\n”);
exit(0);
}else{
echo “Date: “.date(“d-m-Y”).”\n”;
echo “Connecting to asterisk server…..\n”;
$log->debug(“Connecting to asterisk server…..\n”);
}
echo “Connected successfully\n\n\n”;
$asterisk = new Asterisk($sock, $server, $port);

authorizeUser($username, $password, $asterisk);

//keep looping continuosly to check if there are any calls
while (true) {
//check for incoming calls and insert in the database
// sleep(1);
$incoming = handleIncomingCalls($asterisk, $adb);
}
fclose($sock);
unset($sock);
}

/**
* this function checks if there are any incoming calls for the current user
* if any call is found, it just inserts the values into the vtiger_asteriskincomingcalls table
*
* @param $asterisk – the asterisk object
* @param $adb – the peardatabase type object
* @return incoming call information if successful
* false if unsuccessful
*/
function handleIncomingCalls($asterisk, $adb){
global $calls;
$response = $asterisk->getAsteriskResponse(false);
if(empty($response)) {
return false;
}
$tmp = array();
$tmp = explode(“.”, $response['Uniqueid']);
$unique_id = $tmp[0];
if(($response['Event'] == ‘Newstate’) && ($response['State'] == ‘Ring’)) {
if(!array_key_exists($unique_id, $calls)) {
$calls = array($unique_id => array(‘CallerID’ => $response['CallerID'],
‘CallerIDName’ => $response['CallerIDName'],
‘UniqueTime’ => time()));
echo date(“H:i:s”) . ‘ – UniqueId: ‘ . $unique_id . ‘ ==> New Call’ . “\n\r”;
echo ‘ ‘ . ‘ – CallerId: ‘ . $calls[$unique_id]['CallerID'] . “\n\r”;
echo ‘ ‘ . ‘ – CallerName: ‘ . $calls[$unique_id]['CallerIDName'] . “\n\r”;
}
return true;
}
if($response['Event'] == ‘Newcallerid’) {
if(array_key_exists($unique_id, $calls)) {
$calls[$unique_id]['CallerID'] = $response['CallerID'];
$calls[$unique_id]['CallerIDName'] = $response['CallerIDName'];
echo date(“H:i:s”) . ‘ – UniqueId: ‘ . $unique_id . ‘ ==> CallerId Change’ . “\n\r”;
echo ‘ ‘ . ‘ – CallerId: ‘ . $calls[$unique_id]['CallerID'] . “\n\r”;
echo ‘ ‘ . ‘ – CallerName: ‘ . $calls[$unique_id]['CallerIDName'] . “\n\r”;
}
return true;
}
if(($response['Event'] == ‘Newexten’) && (strstr($response['AppData'],”EXTTOCALL”))){
if(array_key_exists($unique_id, $calls)) {
$temp = array();
$temp = explode(“/”,$response['Channel']);
$calls[$unique_id]['CallerType'] = $temp[0];
$temp = explode(“=”,$response['AppData']);
$calls[$unique_id]['Extension'] = $temp[1];
if(checkExtension($calls[$unique_id]['Extension'], $adb)){
$sql = “insert into vtiger_asteriskincomingcalls values (?,?,?,?,?,?)”;
$flag= 0;
$timer = time();
$params = array($calls[$unique_id]['CallerID'],
$calls[$unique_id]['CallerIDName'],
$calls[$unique_id]['Extension'],
$calls[$unique_id]['CallerType'],
$flag, $timer);
$adb->pquery($sql, $params);
addToCallHistory($calls[$unique_id]['Extension'],
$calls[$unique_id]['CallerType'] . “:” . $calls[$unique_id]['CallerID'],
$calls[$unique_id]['Extension'],
“incoming”, $adb);
echo date(“H:i:s”) . ‘ – UniqueId: ‘ . $unique_id . ‘ ==> Internal Call vTiger DB’ . “\n\r”;
echo ‘ ‘ . ‘ – CallerId: ‘ . $calls[$unique_id]['CallerID'] . “\n\r”;
echo ‘ ‘ . ‘ – CallerName: ‘ . $calls[$unique_id]['CallerIDName'] . “\n\r”;
echo ‘ ‘ . ‘ – Extension: ‘ . $calls[$unique_id]['Extension'] . “\n\r”;
}
}
return true;
}
if($response['Event'] == ‘Hangup’){
if(array_key_exists($unique_id, $calls)) {
unset($calls[$unique_id]);
echo date(“H:i:s”) . ‘ – HangUpId: ‘ . $unique_id . ‘ ==> Call End’ . “\n\r”;
}
return true;
}
if(($response['Event'] == ‘Newchannel’) && ($response['State'] == ‘Ring’)){
if(array_key_exists($unique_id, $calls)) {
$temp = array();
$temp = explode(“/”, $response['Channel']);
$temp1 = array();
$temp1 = explode(“@”, $temp[1]);
$calls[$unique_id]['CallerType'] = ‘SIP’;
$calls[$unique_id]['Extension'] = $temp1[0];
echo date(“H:i:s”) . ‘ – UniqueId: ‘ . $unique_id . ‘ ==> New Channel: ‘ . $calls[$unique_id]['Extension'] . “\n\r”;
if(checkExtension($calls[$unique_id]['Extension'], $adb)){
$sql = “insert into vtiger_asteriskincomingcalls values (?,?,?,?,?,?)”;
$flag= 0;
$timer = time();
$params = array($calls[$unique_id]['CallerID'],
$calls[$unique_id]['CallerIDName'],
$calls[$unique_id]['Extension'],
$calls[$unique_id]['CallerType'],
$flag, $timer);
$adb->pquery($sql, $params);
addToCallHistory($calls[$unique_id]['Extension'],
$calls[$unique_id]['CallerType'] . “:” . $calls[$unique_id]['CallerID'],
$calls[$unique_id]['Extension'],
“incoming”, $adb);
echo date(“H:i:s”) . ‘ – UniqueId: ‘ . $unique_id . ‘ ==> External Call vTiger DB’ . “\n\r”;
echo ‘ ‘ . ‘ – CallerId: ‘ . $calls[$unique_id]['CallerID'] . “\n\r”;
echo ‘ ‘ . ‘ – CallerName: ‘ . $calls[$unique_id]['CallerIDName'] . “\n\r”;
echo ‘ ‘ . ‘ – Extension: ‘ . $calls[$unique_id]['Extension'] . “\n\r”;
}
}
return true;
}
return true;
}

/**
* this function takes a XML response and converts it to an array format
* @param string $response – the xml response
* @return the xml formatted into an array
*/
function getArray($xml){
$lines = explode(“\r\n”, $xml);

$response = array();
foreach($lines as $line){
list($key, $value) = explode(“:”, $line);
$response[$key] = $value;
}
return $response;
}

/**
* this function checks if the given extension is a valid vtiger extension or not
* if yes it returns true
* if not it returns false
*
* @param string $ext – the extension to be checked
* @param object $adb – the peardatabase object
*/
function checkExtension($ext, $adb){
$sql = “select * from vtiger_asteriskextensions where asterisk_extension=’$ext’”;
$result = $adb->pquery($sql, array());

if($adb->num_rows($result)>0){
return true;
}else{
return false;
}
}
?>

2 Responses to “vTiger 5.1.0 Asterisk.1.4 integration update”

  1. Dor Says:

    We tryed your post by copy – paste but it raises syntax errors.
    Please provide an alternate link for source download.
    Thanks

  2. Jörn Dreyer Says:

    My PHP may not be the best, but won’t

    $calls = array($unique_id => array(‘CallerID’ => $response['CallerID'],
    ‘CallerIDName’ => $response['CallerIDName'],
    ‘UniqueTime’ => time()));

    always overwrite the “calls cache” with a new array with only one call? Then why even bother with creating a global call array at? Or did you mean to initialize the array in the asteriskClient function earlier with

    global $log, $adb;
    global $calls;

    $calls = array();

    and then add the new call with

    $calls[$unique_id] = array(‘CallerID’ => $response['CallerID'],
    ‘CallerIDName’ => $response['CallerIDName'],
    ‘UniqueTime’ => time()));

    so long

    Jörn

Leave a Reply