#!/usr/bin/php debug("Socket cannot be created due to errno [$errno] - $errstr"); exit(0); } 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")); echo "Connected successfully\n\n"; $asterisk = new Asterisk($sock, $data['server'], $data['port']); # authorize user first authorizeUser($data['username'], $data['password'], $asterisk); // Keep looping to poll the asterisk events while(true) { // Give some break to avoid server hanging sleep(1); try { $incoming = asterisk_handleEvents($asterisk, $adb, $data['version']); asterisk_IncomingEventCleanup($adb); } catch(Exception $ex) { echo "EXCEPTION: " . $ex->getMessage() . "\n"; } } fclose($sock); unset($sock); } /* * Delete the stale incoming events information recorded to avoid * overgrowth of the database. */ function asterisk_IncomingEventCleanup($adb) { $HOURRANGE = 60 * 60; $TIMELIMIT = $HOURRANGE * 12; // Delete events older than 'n' hours $adb->pquery("DELETE FROM vtiger_asteriskincomingevents WHERE timer < ? ", array(time() - $TIMELIMIT) ); } /** * Grab the events from server, parse it and process it. */ function asterisk_handleEvents($asterisk, $adb, $version="1.4") { $fnEntryTime = time(); //values of flag for asteriskincomingevents(-1 for stray calls, 0 for incoming calls, 1 for outgoing call) do { $mainresponse = $asterisk->getAsteriskResponse(); if(!empty($mainresponse)) { $state = ($version == "1.6")? "ChannelStateDesc" : "State"; if(asterisk_handleResponse1($mainresponse, $state, $adb)) { echo "iff1"; //if(asterisk_handleResponse2($mainresponse, $adb, $asterisk, $state)) { echo "iff2"; //if(asterisk_handleResponse3($mainresponse, $adb, $asterisk)){ echo "iff3"; // Proceed if previous event could not be handled. //} //} } } else { // No more response to consume break; } } while(true); return false; } /* The function below will parse the following contents. Event: Dial Privilege: call,all SubEvent: Begin Channel: Local/102@from-queue-5b1a;2 Destination: SIP/102-0000000c CallerIDNum: 0702500267 CallerIDName: alang-nb ConnectedLineNum: 102 ConnectedLineName: 102 UniqueID: 1339727430.35 DestUniqueID: 1339727430.36 Dialstring: 102 */ function asterisk_handleResponse1($mainresponse, $state, $adb) { if( ($mainresponse['Event'] == 'Dial' && $mainresponse['SubEvent'] == 'Begin') ) { $uniqueid = $mainresponse['UniqueID']; if(!empty($mainresponse['CallerIDNum'])) { $callerName = $mainresponse['CallerIDName']; $callerNumber = $mainresponse['CallerIDNum']; $channel = $mainresponse['Channel']; //$toNumber = $mainresponse['ConnectedLineNum']; $toNumber = $mainresponse['Dialstring']; $destination = $mainresponse['Destination']; $splits = explode('/', $destination); $callerType = $splits[0]; $sql = "INSERT INTO vtiger_asteriskincomingevents (uid, channel, from_number, from_name, to_number, callertype, timer, flag) VALUES(?,?,?,?,?,?,?,?)"; $adb->pquery($sql, array($uniqueid, $channel, $callerNumber, $callerName, $toNumber, $callerType, time(), -1)); if(checkExtension($toNumber, $adb)) { if(empty($callerNumber) || $callerNumber == '0') { // Just for in case $sql = "UPDATE vtiger_asteriskincomingevents SET flag=? WHERE uid=?"; $adb->pquery($sql, array(-1, $uniqueid)); } else { $query = "INSERT INTO vtiger_asteriskincomingcalls (refuid, from_number, from_name, to_number, callertype, flag, timer) VALUES(?,?,?,?,?,?,?)"; $adb->pquery($query,array($uniqueid, $callerNumber, $callerName, $toNumber, $callerType, 0, time())); echo "==$uniqueid==$channel==$callerNumber==$callerName=="; } } return false; } } return true; } /** * Check if extension is configured to user in vtiger */ function checkExtension($ext, $adb){ $sql = "select 1 from vtiger_asteriskextensions where asterisk_extension=?"; $result = $adb->pquery($sql, array($ext)); if($adb->num_rows($result)>0){ return true; }else{ return false; } }