Skip to content

Instantly share code, notes, and snippets.

@lgaetz
Last active July 23, 2025 13:57
Show Gist options
  • Select an option

  • Save lgaetz/b350b9fa9ac18730150b387a1525aa4e to your computer and use it in GitHub Desktop.

Select an option

Save lgaetz/b350b9fa9ac18730150b387a1525aa4e to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
if (!isset($argv[2])){
echo "
***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** *****
*
* Script: lgaetz-dnd.php
*
* Latest version: https://gist.github.com/lgaetz/b350b9fa9ac18730150b387a1525aa4e
*
* Usage: Run at bash prompt of FreePBX system running FreePBX 13+ with first argument as
* action (show,set,unset,toggle), and the second argument as extension number
*
* # lgaetz-dnd.php <action> <ext#> [debug]
*
*
* License: GNU/GPL3+
*
* History:
* 2017-11-18 First commit by lgaetz
* 2018-05-29 Added extension validation and debug option
* 2025-07-23 No changes - confirmed working in 17
*
***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** *****
";
exit;
}
include '/etc/freepbx.conf';
$FreePBX = FreePBX::Create();
$debug=false;
if (isset($argv[3])) {
$debug=true;
}
// Check to see if extension is valid
$user=$FreePBX->Core->getUser($argv[2]);
if ($user['extension']!=$argv[2]) {
echo "Error ".$argv[2]." is not a valid extension, exiting.";
exit;
}
// get current dnd status
$status=$FreePBX->Donotdisturb->getStatusByExtension($argv[2]);
switch (strtolower($argv[1])) {
case "show":
if($status=="YES") {
echo"DND Enabled\n";
} else {
echo"DND Disabled\n";
}
break;
case "set":
if($status=="YES") {
output("DND already enabled for ".$argv[2]);
} else {
$device=$FreePBX->Donotdisturb->setStatusByExtension($argv[2],"YES");
output("Enabling DND for ".$argv[2]);
}
break;
case "unset":
if($status=="YES") {
$device=$FreePBX->Donotdisturb->setStatusByExtension($argv[2],"");
output("Disabling DND for ".$argv[2]);;
} else {
output("DND already disabled for ".$argv[2]);
}
break;
case "toggle":
if($status=="YES") {
$device=$FreePBX->Donotdisturb->setStatusByExtension($argv[2],"");
output("Disabling DND for ".$argv[2]);
} else {
$device=$FreePBX->Donotdisturb->setStatusByExtension($argv[2],"YES");
output("Enabling DND for ".$argv[2]);
}
break;
default:
echo "invalid action\n";
exit;
}
function output($string) {
global $debug;
if ($debug) {
echo $string."\n";
}
}
@lgaetz
Copy link
Author

lgaetz commented Jan 9, 2025

@TomServo138 Very clever, I like that idea a lot. The only changes I would propose are:

  1. Call macro-user-callerid dialplan first using either Macro or GoSub depending on fpbx version so that the AMPUSER variable gets defined
  2. Change the system command to use ${AMPUSER} instead of caller id number.

This will catch the edge cases where the caller's caller id number doesn't match the extension number, which could happen when using shadow extensions for webrtc clients, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment