Skip to content

Instantly share code, notes, and snippets.

@hgaibor
Forked from lgaetz/lgaetz-pay-it-fwd.php
Last active June 8, 2020 22:55
Show Gist options
  • Save hgaibor/4c729f69b53bc9090f4c5baf2ed6a879 to your computer and use it in GitHub Desktop.
Save hgaibor/4c729f69b53bc9090f4c5baf2ed6a879 to your computer and use it in GitHub Desktop.
#!/usr/bin/php -q
<?php
if (!isset($argv[2])){
echo "
***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** *****
*
* Script: mass-pay-it-fwd.php
* Based on Script lgaetz-pay-it-fwd.php; modified for changing settings on a batch of extensions
* Sets,unsets,toggles and lists unconditional Call Fwd setting for provided extension
*
* Latest version: https://gist.github.com/hgaibor/58551f0bb3aae20446fa59e98d90917c
* Original Lgaetz version: https://gist.github.com/lgaetz/64212eceab5e2c8381b586ba14df4fcf
*
* Usage: Run at bash prompt of FreePBX system running FreePBX 13+ with arguments:
*
* # php mass-pay-it-fwd.php <command> <ext> <fwd dest>
*
* Commands:
* enable - if disabled, enable for provided extension requires <fwd dest>
* disable - if enabled, disable for provided extension ignores <fwd dest>
* toggle - either enable or disable depending on current state requires <fwd dest> if enabling
* list - show all CF details ignores arg3
* mass-enable - allows to use a CSV file (extension,destination) to enable call forwarding for a set of valid extensions
* mass-disable - allows to use a CSV file (extension,destination) to disable call forwarding for a set of valid extensions
*
* License: GNU/GPL3+
*
* History:
* 2020-04-20 Covid-19 Quarantine - Initial commit
* 2020-04-21 Hgaibor edit - Enabling of batch enabling/disabling
*
***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** *****
";
exit;
}
// user params
$verbose = true; // set to false to diable all console output
// include FreePBX bootstrap environment
include '/etc/freepbx.conf';
$FreePBX = FreePBX::Create();
if((strtolower($argv[1])!="mass-enable")&&(strtolower($argv[1])!="mass-disable")){
// 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 CF status of extension $argv[2]
$foo=$FreePBX->Callforward->getStatusesByExtension($argv[2]);
$current_status=$foo['CF']; // fwd dest if enabled, false if disabled
//get command
switch (strtolower($argv[1])) {
case "enable":
if (isset($argv[3])) {
$device=$FreePBX->Callforward->setNumberByExtension($argv[2],$argv[3]);
echo "CF Unconditional enabled for ".$argv[2]." to ".$argv[3];
exit;
} else {
echo 'CF destination required but not provided - exiting';
exit;
}
break;
case "disable":
$device=$FreePBX->Callforward->setNumberByExtension($argv[2],false);
echo "CF Unconditional disabled for ".$argv[2];
exit;
break;
case "toggle":
if ($current_status) {
$device=$FreePBX->Callforward->setNumberByExtension($argv[2],false);
echo "CF Unconditional disabled for ".$argv[2];
exit;
} else {
if (isset($argv[3])) {
$device=$FreePBX->Callforward->setNumberByExtension($argv[2],$argv[3]);
echo "CF Unconditional enabled for ".$argv[2]." to ".$argv[3];
exit;
} else {
echo 'CF destination required but not provided - exiting';
exit;
}
}
break;
case "list":
if ($current_status) {
echo "CF Unconditional is currently enabled for ".$argv[2]." to ".$current_status;
} else {
echo "CF Unconditional is currently disabled for ".$argv[2];
}
break;
case "mass-enable":
// Hgaibor: Added batch file capabilities
// Date: 2020-April-21
if (isset($argv[2])) {
if (file_exists($argv[2])){
// echo "File exists! \n"; // DEBUGGING ONLY
$ExtListFile = fopen($argv[2], "r") or die("Unable to open file! \n");
// Output one character until end-of-file
while(!feof($ExtListFile)) {
// echo fgetc($ExtListFile); // DEBUGGING ONLY
$CurrentLine=fgets($ExtListFile);
// Removing whitespaces and line breaks before exploding
$ExtEntry=explode(",", rtrim($CurrentLine));
// echo "Ext: ".$ExtEntry[0] ." - Forwarded Number: ". $ExtEntry[1]; // DEBUGGING ONLY
// Check to see if extension is valid
$user=$FreePBX->Core->getUser($ExtEntry[0]);
if ($user['extension']!=$ExtEntry[0]) {
echo "Error ".$ExtEntry[0]." is not a valid extension, skipped. \n";
// exit; // DEBUGGING ONLY
// Then enabling Unconditional Forwarding to each Extension
} else {
// Check if CF destination is set
if (isset($ExtEntry[1])) {
// echo "FW number ok: "; // DEBUGGING ONLY
$device=$FreePBX->Callforward->setNumberByExtension($ExtEntry[0],$ExtEntry[1]);
echo "CF Unconditional enabled for ".$ExtEntry[0]." --> ".$ExtEntry[1]."\n";
} else {
echo "CF destination not provided for Ext".$ExtEntry[0]." - skipped \n";
}
}
}
// Closing opened file
fclose($ExtListFile);
exit;
} else {
echo 'File does not exist';
exit;
}
} else {
echo 'Mass file not specified - exiting';
exit;
}
break;
case "mass-disable":
// Hgaibor: Added batch file capabilities
// Date: 2020-April-21
if (isset($argv[2])) {
if (file_exists($argv[2])){
// echo "File exists! \n"; // DEBUGGING ONLY
$ExtListFile = fopen($argv[2], "r") or die("Unable to open file! \n");
// Output one character until end-of-file
while(!feof($ExtListFile)) {
// echo fgetc($ExtListFile); // DEBUGGING ONLY
$CurrentLine=fgets($ExtListFile);
// Removing whitespaces and line breaks before exploding
$ExtEntry=explode(",", rtrim($CurrentLine));
// echo "Ext: ".$ExtEntry[0] ." - Forwarded Number: ". $ExtEntry[1]; // DEBUGGING ONLY
// Check to see if extension is valid
// Then enabling Unconditional Forwarding to each Extension
$user=$FreePBX->Core->getUser($ExtEntry[0]);
if ($user['extension']!=$ExtEntry[0]) {
echo "Error ".$ExtEntry[0]." is not a valid extension, skipped. \n";
// exit; // DEBUGGING ONLY
} else {
$device=$FreePBX->Callforward->setNumberByExtension($ExtEntry[0],false);
echo "CF Unconditional disabled for ".$ExtEntry[0]."\n";
}
}
// Closing opened file
fclose($ExtListFile);
exit; // DEBUGGING ONLY
} else {
echo 'File does not exist';
exit;
}
} else {
echo 'Batch file not specified - exiting';
exit;
}
break;
}
exit ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment