Set 'Call Forward on Busy' to Voicemail for all Extensions. (Example #15)ScopServ Base Library
In order to create Mass Operations scripts, you must define some variables and load libraries. To help you in this task, we create a simple script. Save this file as /var/www/scopserv/telephony/contrib/base.php and include it on your scripts.
<?php
// No auth.
@define('AUTH_HANDLER', true);
@define('TELEPHONY_COMMIT', true);
ini_set('include_path', '/var/www/scopserv/framework');
@define('TELEPHONY_BASE', '/var/www/scopserv/telephony');
require_once TELEPHONY_BASE . '/lib/base.php';
require_once 'Console/Getopt.php';
require_once 'Horde/CLI.php';
require_once 'Horde/Array.php';
// Load the CLI environment - make sure there's no time limit, init
// some variables, etc.
$c = &new Horde_CLI();
// Make sure no one runs this from the web.
if (!$c->runningFromCLI()) {
$c->fatal(_("This script must be run from the command line."));
}
$c->init();
/* Make sure there's no compression. */
@ob_end_clean();
Disable VM envelope and change password from 1234 to random.
#!/usr/bin/php -q
<?php
require_once '/var/www/scopserv/telephony/contrib/base.php';
/* Get all users from backend. */
$infos = $telephony->getExtensions('phone', null, true, false);
foreach($infos as $id => $info) {
/* Set default value to false */
$changed = false;
/* Get info Extension number */
$ext = $info['phone_extension'];
print "Extension $ext ...\n";
/* Check if Voicemail Envelope is defined and set it to false */
if (isset($info['phone_vm_envelope'])) {
$info['phone_vm_envelope'] = false;
$changed = true;
print " - VM Envelope disabled.\n";
}
/* If the Password (SIP) is set to 1234, changed it to random password */
if (isset($info['phone_password']) && $info['phone_password'] == '1234') {
$newpass = generatePassword();
$info['phone_password'] = $newpass;
print " - Password changed to $newpass\n";
$changed = true;
}
/* If infos have changed, save it */
if ($changed) {
$ext_id = $telephony->saveExtension($info);
}
print "Done.\n\n";
}
/* Generate a random password with alphanumeric */
function generatePassword() {
/* Password length (default: 6) */
$length = 6;
/* Prefix */
$pass = '';
/* Characters set */
$chars = "abcdefghijkmnopqrstuvwxyz023456789";
$i = strlen($pass);
srand((double)microtime()*1000000);
while ($i <= $length) {
$num = rand() % 33;
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
/* Return generated password */
return $pass;
}
List all codecs information for each extensions.
#!/usr/bin/php -q
<?php
require_once '/var/www/scopserv/telephony/contrib/base.php';
/* Get all Phones from backend. */
$infos = $telephony->getExtensions('phone', null, true, false);
foreach($infos as $id => $info) {
if (!isset($info['phone_codec'])) {
continue;
}
print "Extension: " . $info['phone_extension'] . "\n";
print "Codec : " . implode(', ', $info['phone_codec']) . "\n";
print "\n";
}
Set codecs for all extensions to 'ulaw', 'g729' and 'gsm'.
#!/usr/bin/php -q
<?php
require_once '/var/www/scopserv/telephony/contrib/base.php';
/* Get all Phones from backend. */
$infos = $telephony->getExtensions('phone', null, true, false);
foreach($infos as $id => $info) {
if (!isset($info['phone_codec'])) {
continue;
}
$info['phone_codec'] = array('ulaw', 'g729', 'gsm');
$telephony->saveExtension($info);
}
Disable Call Limit for all extensions.
#!/usr/bin/php -q
<?php
require_once '/var/www/scopserv/telephony/contrib/base.php';
/* Get all Phones from backend. */
$infos = $telephony->getExtensions('phone', null, true, false);
foreach($infos as $id => $info) {
if (!isset($info['phone_limit_in'])) {
continue;
}
$info['phone_limit_in'] = '';
$info['phone_limit_out'] = '';
$info['phone_limit_sip_call'] = '';
$telephony->saveExtension($info);
}
Enable Call Transfer on all Incoming Lines.
#!/usr/bin/php -q
<?php
require_once '/var/www/scopserv/telephony/contrib/base.php';
/* Get all Incoming Lines from backend. */
$infos = $telephony->getExtensions('line_in', null, true, false);
foreach($infos as $id => $info) {
/* Get the destination */
$dest = $info['line_in_destination'];
/* Check if 'Use User-CallForward' is enabled */
$uselocal = @$info['line_in_destination_ext_uselocal'];
/* If Destination is 'Extension' and 'UseLocal' is not enabled */
if ($dest == 'ext' && !$uselocal) {
/* Enable 'Call Transfer' to 'Callee' */
$info['line_in_destination_ext_transfer'] = 'callee';
/* Save Informations */
$telephony->saveExtension($info);
}
}
Command-line script to add Speed Dial.
#!/usr/bin/php -q
<?php
require_once '/var/www/scopserv/telephony/contrib/base.php';
/* Add Speeddial Information */
$args = Console_Getopt::readPHPArgv();
$info = array();
$info['type'] = 'speeddial';
$info['speeddial_ext'] = $args[1];
$info['speeddial_name'] = $args[2];
$info['speeddial_number'] = $args[3];
$ext_id = $telephony->saveExtension($info);
Enable 'Use User-Local CallForward' for all Incoming Lines.
#!/usr/bin/php -q
<?php
require_once '/var/www/scopserv/telephony/contrib/base.php';
/* Add Speeddial Information */
foreach($telephony->getExtensions('line_in', false) as $id => $info) {
$dest = $info['line_in_destination'];
if ($dest == 'ext') {
// print "$dest\n";
$fb = isset($info['line_in_fallback']) ? $info['line_in_fallback'] : 'none';
if ($fb == 'none') {
$info['line_in_destination_ext_uselocal'] = true;
$ext_id = $telephony->saveExtension($info);
}
}
}
Enable 'DUNDi' for all extensions
#!/usr/bin/php -q
<?php
require_once '/var/www/scopserv/telephony/contrib/base.php';
// Loop through each phone extensions
foreach($telephony->getExtensions('phone', false) as $id => $info) {
if (in_array($info['phone_type'], array('vm', 'fax', 'agent'))) {
continue;
}
$info['phone_dundi'] = true;
$info['phone_dundi_number'] = $info['phone_extension'];
$info['phone_dundi_context'] = '_default_';
$telephony->saveExtension($info);
}
Set Maximum RingTime to 15 seconds for extensions 1000 to 5000.
#!/usr/bin/php -q
<?php
require_once '/var/www/scopserv/telephony/contrib/base.php';
// Loop through all phone extensions
foreach($telephony->getExtensions('phone', false) as $id => $info) {
$ext = $info['phone_extension'];
if ($ext >= 1000 && $ext <= 5000) {
echo sprintf("Ext %s: Ringtime %d -> %d\n", $ext, $info['phone_maxtime'], 15);
$info['phone_maxtime'] = 15;
$telephony->saveExtension($info);
}
}
Change VM Password and Web GUI to match the extension number.
#!/usr/bin/php -q
<?php
require_once '/var/www/scopserv/telephony/contrib/base.php';
/* Get all users from backend. */
$infos = $telephony->getExtensions('phone', null, true, false);
foreach($infos as $id => $info) {
/* Set default value to false */
$changed = false;
/* Get info Extension number */
$ext = $info['phone_extension'];
print "Extension $ext ...\n";
/* If the Voicemail is enable, change password to match the extension */
if (isset($info['phone_vm']) && $info['phone_vm']) {
$info['phone_vm_password'] = $ext;
print " - Password changed to $ext\n";
$changed = true;
}
/* If Web GUI is enable, change the username to match the extension number */
if (isset($info['phone_user_gui']) && $info['phone_user_gui']) {
$info['phone_web_username'] = $ext;
print " - Web GUI username changed to $ext\n";
$changed = true;
}
/* If infos have changed, save it */
if ($changed) {
$ext_id = $telephony->saveExtension($info);
}
print "Done.\n\n";
}
How to import Whitelist data on SQL for Outgoing Call restrictions.
#!/usr/bin/php -q
<?php
/*
Table structure for table 'whitelist'
-------------------------------------------
CREATE TABLE whitelist (
phone_number varchar(32) NOT NULL,
phone_name varchar(64) NOT NULL,
phone_type varchar(32) NOT NULL,
PRIMARY KEY (phone_number)
) TYPE=MyISAM;
CREATE INDEX phone_number ON whitelist (phone_number);
Record structure for file 'whitelist.txt'
-------------------------------------------
NUMBER;NAME;TYPE
8195551212;John Doe;PRIVATE
5145551234;Mary Jane;PRIVATE
5145550000;ACME Inc.;COMPANY
*/
require_once '/var/www/scopdev/telephony/contrib/base.php';
/* Record separator (; or ,) */
$sep = ';';
$table = 'whitelist';
$file = 'whitelist.txt';
if (!@file_exists($file)) {
print "Missing file: $file\n";
exit;
}
$handle = fopen($file, "r");
while (($data = fgetcsv($handle, 1000, $sep)) !== FALSE) {
$num = (string)$data[0];
$name = $data[1];
$type = $data[2];
$sql_sprintf = 'INSERT INTO %s SET phone_number = %s, phone_name = %s, phone_type = %s';
$sql = sprintf($sql_sprintf,
$table,
$telephony->_db->quote($num),
$telephony->_db->quote($name),
$telephony->_db->quote($type));
print "Added $num to $table ... ";
$telephony->_db->query($sql);
print "done\n";
}
Microbrowser support is awesome! Now I have thousands of phones to enable microbrowser.
#!/usr/bin/php -q
<?php
/* Microbrowser support is awesome!
*
* Now I have thousands of phones to enable microbrowser.
*
* Please post script for phone APS and usage docs for the following:
*
* - Use server LAN IP to configure internal microbrowser services.
* - Use extension assigned to line 1 as extension to monitor
* - Enable phone status and ACD status options for microbrowser
**/
// Ok, for this script, I have add both method to load the base
// library config. If the base.php file exist on the contrib directory
// it will load base config, else it use one defined on this script.
// Not really useful but showed as example.
if (file_exists('/var/www/scopserv/telephony/contrib/base.php')) {
require_once '/var/www/scopserv/telephony/contrib/base.php';
} else {
// No auth.
@define('AUTH_HANDLER', true);
@define('ASTERISK_COMMIT', true);
ini_set('include_path', '/var/www/scopserv/framework');
@define('ASTERISK_BASE', '/var/www/scopserv/telephony');
require_once ASTERISK_BASE . '/lib/base.php';
require_once 'Console/Getopt.php';
require_once 'Horde/CLI.php';
require_once 'Horde/Array.php';
// Load the CLI environment - make sure there's no time limit, init
// some variables, etc.
$c = &new Horde_CLI();
// Make sure no one runs this from the web.
if (!$c->runningFromCLI()) {
$c->fatal(_("This script must be run from the command line."));
}
$c->init();
/* Make sure there's no compression. */
@ob_end_clean();
}
/* Get Network Configurations */
$server_configs = $scopserv->getConfig('config');
/* Get Network Configurations */
$network_configs = $scopserv->getConfig('network');
/* Set informations for Server URL */
// Check if we use HTTP or HTTPS (SSL) and use HTTP as default
$proto = @$server_configs['config_ssl'] ? 'https' : 'http';
// Get IP Address for LAN (use config_wan_ipaddr for WAN)
$server = implode('.', $network_configs['config_lan_ipaddr']);
// Get Server Port from config and if use 5555 as default
$port = @$server_configs['config_port'] ? $server_configs['config_port'] : 5555;
/* Get all Auto Provisioning System (APS) informations from backend. */
$infos = $telephony->getExtensions('aps', null, true, false);
foreach($infos as $id => $info) {
// Status if informations have changed. Set default value to false.
$changed = false;
// Get Phone Type
$type = $info['aps_aps_type'];
// Check if Internal MicroBrowser variable is available
if (isset($info['aps_mb_internal'])) {
// Enable Internal MicroBrowser
$info['aps_mb_internal'] = true;
/* Set informations for Server URL */
// Check if we use HTTP or HTTPS (SSL) and use HTTP as default
$info['aps_mb_internal_proto'] = @$server_configs['config_ssl'] ? 'https' : 'http';
// Get IP Address for LAN (use config_wan_ipaddr for WAN)
$info['aps_mb_internal_srv'] = implode('.', $network_configs['config_lan_ipaddr']);
// Get Server Port from config and if use 5555 as default
$info['aps_mb_internal_port'] = @$server_configs['config_port'] ? $server_configs['config_port'] : 5555;
// Enable Phone and ACD status permissions for MicroBrowser
$info['aps_mb_internal_show_acd'] = true;
$info['aps_mb_internal_show_hotdesk'] = true;
$info['aps_mb_internal_show_spy'] = false;
// Use extension assigned to line 1 as extension to monitor
// Special case for polycom phone that doesn't use same variable
if (isset($info['aps_polycom_button_1'])) {
$ext = $info['aps_polycom_button_1'];
} elseif (isset($info['aps_button_1'])) {
$ext = $info['aps_button_1'];
} else {
$ext = false;
}
// Set Extension to Monitor
$info['aps_mb_internal_ext'] = $ext;
// Set the status to Changed if an extension is defined
if ($ext) {
// Change status set to true
$changed = true;
// Print information about Extension
print "Enable Internal Microbrowser on Extension $ext ($type) ...\n";
}
}
/* If infos have changed, save it */
if ($changed) {
$ext_id = $telephony->saveExtension($info);
}
}
Enable Voicemail and MWI for all extensions. Use extension as password.
#!/usr/bin/php -q
<?php
require_once '/var/www/scopserv/telephony/contrib/base.php';
/* Get all users from backend. */
$infos = $telephony->getExtensions('phone', null, true, false);
foreach($infos as $id => $info) {
/* Set default value to false */
$changed = false;
/* Get info Extension number */
$ext = $info['phone_extension'];
print "Extension $ext ...\n";
/* Enable Voicemail if it disabled */
if (isset($info['phone_vm']) && !$info['phone_vm']) {
$info['phone_vm'] = true; // Enable Voicemail
$info['phone_usemwi'] = true; // Enable MWI
$info['phone_vm_password'] = $ext; // Set Password as Extension
$changed = true;
print " - Voicemail Enabled.\n";
}
/* If infos have changed, save it */
if ($changed) {
$ext_id = $telephony->saveExtension($info);
}
print "Done.\n\n";
}
External (SQL) CallerID Lookup on Outgoing Lines.
#!/usr/bin/php -q
<?php
/*
Table structure for table 'cidlookup'
-------------------------------------------
CREATE TABLE cidlookup (
phone_number varchar(32) NOT NULL,
calleridnum varchar(32) NOT NULL,
calleridname varchar(64) NOT NULL,
calleridpres varchar(32) NOT NULL,
PRIMARY KEY (phone_number)
) TYPE=MyISAM;
CREATE INDEX phone_number ON cidlookup (phone_number);
Record structure for file 'cidlookup.txt'
-------------------------------------------
NUMBER;CIDNUM;CIDNAME;CIDPRES
1234;8195551212;John Doe;allowed_not_screened
1000;5145551234;Mary Jane;allowed
5555;5145550000;ACME Inc.;
*/
require_once '/var/www/scopdev/telephony/contrib/base.php';
/* Record separator (; or ,) */
$sep = ';';
$table = 'cidlookup';
$file = 'cidlookup.txt';
if (!@file_exists($file)) {
print "Missing file: $file\n";
exit;
}
$handle = fopen($file, "r");
while (($data = fgetcsv($handle, 1000, $sep)) !== FALSE) {
$num = (string)$data[0];
$cnum = $data[1];
$cname = $data[2];
$cpres = $data[3];
$sql_sprintf = 'INSERT INTO %s SET phone_number = %s, calleridnum = %s, calleridname = %s, calleridpres = %s';
$sql = sprintf($sql_sprintf,
$table,
$telephony->_db->quote($num),
$telephony->_db->quote($cnum),
$telephony->_db->quote($cname),
$telephony->_db->quote($cpres));
print "Added $num to $table ... ";
$telephony->_db->query($sql);
print "done\n";
}
Set 'Call Forward on Busy' to Voicemail for all Extensions.
<?php
// Load default libraries
require_once '/var/www/scopserv/telephony/contrib/base.php';
/* Get all users from backend. */
$infos = $telephony->getExtensions('phone', null, true, false);
foreach($infos as $id => $info) {
// Set default value to false
$changed = false;
// Get info Extension number
$ext = $info['phone_extension'];
// Check if Voicemail is enabled on the extension
if (isset($info['phone_vm']) && $info['phone_vm']) {
// Display the current extension
print "Extension $ext ...\n";
// Check if CF on Busy is defined and not set to 'none'
if (isset($info['phone_user_cfob']) && $info['phone_user_cfob'] == 'none') {
print " - CallForward on Busy set to Voicemail ($ext)\n";
// Set new information for CFOB
$info['phone_user_cfob'] = 'vmail';
$info['phone_user_cfob_vmail'] = $ext . '/';
$info['phone_user_cfob_vmail_msg'] = 'busy';
// Set change flag to true
$changed = true;
}
}
/* If infos have changed, save it */
if ($changed) {
// Save information on database
$ext_id = $telephony->saveExtension($info);
}
}