Last active
February 20, 2019 14:56
-
-
Save lgaetz/bd08762103cacbecc27b4d8785f51413 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
/*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** | |
* | |
* Script: lgaetz-cmadd.php | |
* | |
* Latest version: https://gist.github.com/lgaetz/bd08762103cacbecc27b4d8785f51413 | |
* | |
* Usage: Asterisk AGI file to add a FreePBX Contact Manager contact | |
* | |
* AGI(lgaetz-cmadd.php,<contact_nuumber>[,<contact_name>,<group_name>]) | |
* | |
* contact_nuumber - Number for new contact | |
* contact_name - Display name for new contact - defaults to whitelist | |
* group_name - Name of Contact Management group to add new entry - defaults to whitelist | |
* group will be created if it doesn't exist | |
* | |
* use with FreePBX Custom Destintion with a goto string of "add-cid-to-whitelist,s,1" without quotes | |
* and something like the following in extensions_custom.conf | |
* | |
* [add-cid-to-whitelist] | |
* exten => s,1,Noop(Entering user defined context [add-cid-to-whitelist] in extensions_custom.conf) | |
* exten => s,n,set(name=${STRREPLACE(CALLERID(name),\,)}) ; remove commas from name before passing as agi arg | |
* exten => s,n,AGI(lgaetz-cmadd.php,${CALLERID(number)},${name}) | |
* exten => s,n,Return | |
* | |
* To automatically add outbound dialed numbers to the whitelist: | |
* | |
* [macro-dialout-trunk-predial-hook] | |
* exten => s,1,Noop(Entering user defined context [macro-dialout-trunk-predial-hook] in extensions_custom.conf) | |
* exten => s,n,AGI(lgaetz-cmadd.php,${OUTNUM:-10}) ; normalize numbers to 10 digits | |
* exten => s,n,MacroExit | |
* | |
* License: GNU/GPL3+ | |
* | |
* History: | |
* 2019-01-13 First commit by lgaetz - not fully functional | |
* 2019-01-20 Add AGI components | |
* 2019-01-27 updated usage sample dialplan | |
* 2019-02-20 added additional check for null arg1 | |
* | |
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***/ | |
// FreePBX Bootstrap environment | |
include '/etc/freepbx.conf'; | |
$FreePBX = FreePBX::Create(); | |
// AGI Class | |
require_once "phpagi.php"; | |
$AGI = new AGI(); | |
if (!$AGI || !$FreePBX) { | |
// something is wrong, exit | |
exit; | |
} | |
// TODO: Check to see if Contact Management is installed | |
// script requires a contact number - exit if not provided | |
if (isset($argv[1]) & strlen($argv[1])!=0) { | |
$contact_number = $argv[1]; | |
} else { | |
agi_verbose("No number provided, exiting"); | |
exit; | |
} | |
// get contact name argument | |
if (strlen($argv[2])!=0) { | |
$contact_name = $argv[2]; | |
} else { | |
$contact_name = "whitelist"; | |
} | |
// get group name argument | |
if (strlen($argv[3])!=0) { | |
$group_name = $argv[3]; | |
} else { | |
$group_name = "whitelist"; | |
} | |
// get contact group id from name | |
$group_id=getGroupIDFromName($group_name); | |
// if contact group doesn't exist, create it | |
if (!isset($group_id)) { | |
$groupadd=$FreePBX->Contactmanager->addGroup($group_name,'external'); | |
if($groupadd['type']=='success') { | |
agi_verbose("Success: ".$groupadd['message']); | |
$group_id=$groupadd['id']; | |
} else { | |
agi_verbose("Failure: ".$groupadd['message'].". Exiting."); | |
exit; | |
} | |
} | |
// get list of all numbers in group | |
$group_numbers = AllNumbersInGroup($group_id); | |
// determine if contact already exists in this group | |
if (in_array($contact_number,$group_numbers)) { | |
// number already exists in this group, don't add again | |
agi_verbose("$contact_number already exists in Contact Mgr group $group_name, exiting"); | |
exit; | |
} | |
// at this point group exists, and contact doesn't. Add new contact | |
$new_contact = array( | |
'displayname' => $contact_name, | |
'numbers'=> array( | |
'1' => array( | |
'number' => $contact_number, | |
'type' => 'other', | |
), | |
), | |
); | |
$entry=$FreePBX->Contactmanager->addEntryByGroupID($group_id, $new_contact); | |
if ($entry['type'] == 'success') { | |
agi_verbose("Success, $contact_number added to Contact Mgr group $group_name"); | |
} else { | |
agi_verbose("Failure, $contact_number NOT added to Contact Mgr group $group_name"); | |
} | |
// helper functions | |
// returns group id from provided name or null if it doesn't exist | |
function getGroupIDFromName ($group_name) { | |
$FreePBX = FreePBX::Create(); | |
$groups=$FreePBX->Contactmanager->getGroups(); // get array of all cm groups | |
foreach ($groups as $group) { | |
if ($group['name'] == $group_name) { | |
// group exists | |
Return($group['id']); | |
} | |
} | |
Return null; | |
} | |
// returns simple keyless array of all phone numbers in specified group | |
function AllNumbersInGroup($group_id) { | |
$FreePBX = FreePBX::Create(); | |
// get all number details from group | |
$number_details=$FreePBX->Contactmanager->getNumbersByGroupID($group_id); | |
// create simple array of all numbers in group | |
$numbers = array(); | |
foreach (array_values($number_details) as $entry) { | |
if (isset($entry['number'])) { | |
$numbers[] = $entry['number']; | |
} | |
} | |
return $numbers; | |
} | |
function agi_verbose($string, $level=3) { | |
global $AGI; | |
$AGI->verbose($string, $level); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment