Skip to content

Instantly share code, notes, and snippets.

@reetp
Created September 8, 2017 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reetp/1dd2f84daa29f5f36cd777fd81460731 to your computer and use it in GitHub Desktop.
Save reetp/1dd2f84daa29f5f36cd777fd81460731 to your computer and use it in GitHub Desktop.
vTiger Asterisk Event Logger
#!/usr/bin/php
<?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.
************************************************************************************/
@ini_set('error_reporting', E_WARNING & ~E_NOTICE);
// chdir('../../../');
// In case chdir is not permitted
ini_set('include_path', '../../../');
require_once ('config.inc.php');
require_once ('include/utils/utils.php');
require_once ('include/language/en_us.lang.php');
require_once ('modules/PBXManager/utils/AsteriskClass.php');
require_once ('modules/PBXManager/AsteriskUtils.php');
main__asteriskClient();
function main__asteriskClient()
{
global $app_strings, $current_user, $adb, $log;
$data = getAsteriskInfo($adb);
$errno = $errstr = null;
$sock = @fsockopen($data['server'], $data['port'], $errno, $errstr, 1);
if ($sock === false) {
echo "Socket cannot be created due to errno [$errno] - $errstr";
$log->debug("Socket cannot be created due to errno [$errno] - $errstr");
exit(0);
}
stream_set_blocking($sock, false);
echo "Connecting to asterisk server @ " . date("Y-m-d H:i:s") . "\n";
$log->debug("Connecting to asterisk server @ " . date("Y-m-d H:i:s"));
$asterisk = new Asterisk($sock, $data['server'], $data['port']);
// authorize user first
authorizeUser($data['username'], $data['password'], $asterisk);
echo "Connected successfully\n\n";
// Keep looping to poll the asterisk events
while (true) {
// Give some break to avoid server hanging
usleep(1000);
try {
$incoming = asterisk_handleEvents($asterisk, $adb, $data['version']);
// asterisk_IncomingEventCleanup($adb);
}
catch(Exception $ex) {
echo "EXCEPTION: " . $ex->getMessage() . "\n";
}
}
fclose($sock);
unset($sock);
}
/**
* Grab the events from server, parse it and process it.
*/
function asterisk_handleEvents($asterisk, $adb, $version = '1.4')
{
$fnEntryTime = time();
// PHP uses ?: to set defaults
$state = ($version == '1.6') ? 'ChannelStateDesc' : 'State';
//values of flag for asteriskincomingevents(-1 for stray calls, 0 for incoming calls, 1 for outgoing call)
do {
// This is getting the info from Asterisk
$mainresponse = $asterisk->getAsteriskResponse();
echo "\nreetp do loop Main Response is: " . date("Y-m-d H:i:s") . "\n";
echo "\n";
$myFile = "testFile.txt";
$writeable = is_writable($myFile) or die("Not writeable\n");
if (!empty($mainresponse)) {
echo "reetp try Response 1\n";
if ($mainresponse['Event'] == 'Newstate' || $mainresponse['Event'] == 'Newchannel' || $mainresponse['Event'] == 'Newexten' || $mainresponse['Event'] == 'Dial' || $mainresponse['Event'] == 'Link' || $mainresponse['Event'] == 'Bridge') {
echo "writing file\n";
echo "File " . $myFile . "\n";
if (!$handle = fopen($myFile, 'a')) {
echo "Cannot open file ($myFile)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, print_r($mainresponse, true)) === FALSE) {
echo "Cannot write to file ($myFile)";
exit;
}
echo "Success, wrote ($mainresponse) to file ($myFile)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
// $fh = fopen($myFile, 'a') or die("can't open file");
// fwrite($fh, $mainresponse);
// fclose($fh);
// print_r($mainresponse);
} else {
// No more response to consume
break;
}
} while (true);
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment