Skip to content

Instantly share code, notes, and snippets.

@lgaetz
Last active January 16, 2024 20:30
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 lgaetz/4c17fa9f27e060321a32beee9a3a5ced to your computer and use it in GitHub Desktop.
Save lgaetz/4c17fa9f27e060321a32beee9a3a5ced to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
// script requires a command - echo help and exit if arg1 missing
if (!isset($argv[1])) {
echo "
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
*
* Script: lgaetz-cftoggle.php
*
* Latest version: https://gist.github.com/lgaetz/4c17fa9f27e060321a32beee9a3a5ced
*
* Usage: Scipt to list/toggle/set FreePBX Call Flow Controls. Developed for FreePBX
* 16, will probably work for earlier versions
*
* lgaetz-cftoggle <command> [<id>] [DAY|NIGHT]
*
* commands - list echos all Call Flows with ID, all other args ignored
* - toggle toggles Call Flow, requires id as 2nd argument
* - set sets Call Flow to desired setting, requires id as 2nd argument and DAY or NIGHT as 3rd arg
*
* License: GNU/GPL3+
*
* History:
* 2022-10-31 All Hallows Eve Edition
*
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
\n
";
exit;
}
// FreePBX Bootstrap environment
include '/etc/freepbx.conf';
$FreePBX = FreePBX::Create();
switch (strtolower($argv[1])) {
case 'list':
// get list of all time conditions on system and output summary to console
$cflist = $FreePBX->Daynight->listCallFlows();
echo "Idx CF Description State\n";
foreach ($cflist as $cfitem) {
$cfstate = getcfstate($cfitem['ext']);
echo $cfitem['ext']." ".str_pad($cfitem['dest'],20)." ".$cfstate."\n";
}
exit;
case 'toggle':
if (!isset($argv[2])) {
echo "toggle command requires ID\n";
exit;
}
// TODO: check if ID is valid
// get current state of call flow and change to opposite
$cfstate = getcfstate($argv[2]); // returns "DAY" or "NIGHT"
if ($cfstate=='DAY') {
$toggle = $FreePBX->Daynight->setState($argv[2],'NIGHT');
} else {
$toggle = $FreePBX->Daynight->setState($argv[2],'DAY');
}
// todo: check state after and confirm change to screen
break;
case 'set':
if ($argv[3]!="DAY" and $argv[3]!="NIGHT") {
echo "set command requires DAY or NIGHT\n";
exit;
}
// TODO: check if ID is valid
setcfstate($argv[2],$argv[3]);
// todo: check state after and echo confirmation
exit;
default:
echo "unknown command \n";
exit;
}
exit;
function getcfstate($id) {
global $FreePBX;
return $FreePBX->Daynight->getState($id);
}
function setcfstate($id,$state=null) {
global $FreePBX;
$foo = $FreePBX->Daynight->setState($id,$state);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment