extimport

#!/usr/bin/env php
<?php
 
/****************************************************/
/* */
/* Copyright (C) 2016 Ringfree Communications LLC */
/* By Kendall Weaver <kendall@ringfree.com> */
/* */
/****************************************************/
 
 
// We need to load the freepbx.conf file in order to access the database information.
require_once('/etc/freepbx.conf');
 
 
// Quick sanity check on the database information.
$dbserver = $amp_conf['AMPDBHOST'];
$username = $amp_conf['AMPDBUSER'];
$password = $amp_conf['AMPDBPASS'];
$database = $amp_conf['AMPDBNAME'];
 
 
// Make sure that at least one argument variable exists before moving forward.
if(!$argv[1]) {
echo "Usage: extimport [FILE]\n";
exit(0);
}
 
 
// Make sure that the first argv exists and that it's a file.
if (file_exists($argv[1])) {
 
// Create an array from the lines within the file.
$csv = fopen($argv[1], 'r');
$data = str_getcsv(fread($csv, filesize($argv[1])), "\n");
fclose($csv);
 
// Create sub-arrays using the CSV data.
foreach ($data as &$row) {
$row = str_getcsv($row);
}
 
// Unset the $row variable so we can properly iterate through it later.
unset($row);
 
// If the first argv isn't a file, exit with status 1.
} else {
echo "Please specify a valid file.\n";
exit(1);
}
 
 
// Connect to the database.
try {
$conn = new PDO("mysql:host=$dbserver;dbname=$database", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 
// Iterate through each row of the CSV data.
foreach ($data as $row) {
 
if ($row[2] != "") {
$name = str_replace(" ", "\\ ", $row[2]);
}
 
if ($row[4] == "1") {
$vm = "default";
} else {
$vm = "novm";
}
 
$extdel = 'DELETE FROM users WHERE extension = "' . $row[1] . '"';
$extins = 'INSERT INTO users (extension,password,name,voicemail,ringtimer,noanswer,recording,outboundcid,sipname,noanswer_cid,busy_cid,chanunavail_cid,noanswer_dest,busy_dest,chanunavail_dest) values ("' . $row[1] . '", "", "' . $name . '", "' . $vm . '", "0", "", "", "' . $row[3] . '", "", "", "", "", "", "", "")';
 
$conn->exec($extdel);
$conn->exec($extins);
 
// Actions to perform if this is a SIP extension.
if ($row[0] == "sip") {
 
// MySQL
$devdel = 'DELETE FROM devices WHERE id = "' . $row[1] . '"';
$devins = 'INSERT INTO devices (id,tech,dial,devicetype,user,description,emergency_cid) values ("' . $row[1] . '","sip","SIP/' . $row[1] . '","fixed","' . $row[1] . '","' . $name . '","' . $row[10] . '")';
$sipdel = 'DELETE FROM sip WHERE id="' . $row[1] . '"';
 
$conn->exec($devdel);
$conn->exec($devins);
$conn->exec($sipdel);
 
// SIP Stuff
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"secret","' . $row[7] . '",2)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"dtmfmode","rfc2833",3)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"canreinvite","yes",4)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"context","from-internal",5)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"host","dynamic",6)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"trustrpid","yes",7)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"sendrpid","no",8)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"type","friend",9)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"nat","yes",10)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"port","5060",11)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"qualify","yes",12)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"qualifyfreq","60",13)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"transport","udp",14)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"encryption","no",15)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"callgroup","",16)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"pickupgroup","",17)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"disallow","",18)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"allow","",19)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"dial","SIP/' . $row[1] . '",20)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"accountcode","",21)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"mailbox","' . $row[1] . '@default",22)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"deny","0.0.0.0/0.0.0.0",23)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"permit","0.0.0.0/0.0.0.0",24)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"account","' . $row[1] . '",25)');
$conn->exec('INSERT INTO sip (id, keyword, data, flags) values (' . $row[1] . ',"callerid","device <' . $row[1] . '>",26)');
 
// AstDB
exec('asterisk -rx "database put AMPUSER/' . $row[1] . ' device ' . $row[1] . '"');
exec('asterisk -rx "database put DEVICE/' . $row[1] . ' default_user ' . $row[1] . '"');
exec('asterisk -rx "database put DEVICE/' . $row[1] . ' dial SIP/' . $row[1] . '"');
exec('asterisk -rx "database put DEVICE/' . $row[1] . ' type fixed"');
exec('asterisk -rx "database put DEVICE/' . $row[1] . ' user ' . $row[1] . '"');
}
 
// Voicemail
if ($row[4] == "1") {
 
if ($row[6]) {
$email = "yes";
} else {
$email = "no";
}
 
$vmstring = $row[1] . " => " . $row[5] . "," . $row[2] . "," . $row[6] . ",,attach=" . $email . "|saycid=no|envelope=no|delete=no\n[other]";
$vmfile = file_get_contents('/etc/asterisk/voicemail.conf');
$vmfile = str_replace("[other]", $vmstring, $vmfile);
file_put_contents('/etc/asterisk/voicemail.conf', $vmfile);
}
 
// AstDB stuff for both SIP and Virtual extensions
exec('asterisk -rx "database put AMPUSER/' . $row[1] . ' answermode disabled"');
exec('asterisk -rx "database put AMPUSER/' . $row[1] . '/ccss cc_agent_policy generic"');
exec('asterisk -rx "database put AMPUSER/' . $row[1] . '/ccss cc_offer_timer 30"');
exec('asterisk -rx "database put AMPUSER/' . $row[1] . ' cfringtimer 0"');
exec('asterisk -rx "database put AMPUSER/' . $row[1] . ' cidnum ' . $row[1] . '"');
exec('asterisk -rx "database put AMPUSER/' . $row[1] . ' concurrency_limit 0"');
exec('asterisk -rx "database put AMPUSER/' . $row[1] . '/queues qnostate usestate"');
exec('asterisk -rx "database put AMPUSER/' . $row[1] . '/recording/in external dontcare"');
exec('asterisk -rx "database put AMPUSER/' . $row[1] . '/recording/in internal dontcare"');
exec('asterisk -rx "database put AMPUSER/' . $row[1] . '/recording ondemand enabled"');
exec('asterisk -rx "database put AMPUSER/' . $row[1] . '/recording/out external dontcare"');
exec('asterisk -rx "database put AMPUSER/' . $row[1] . '/recording/out internal dontcare"');
exec('asterisk -rx "database put AMPUSER/' . $row[1] . '/recording priority 10"');
exec('asterisk -rx "database put AMPUSER/' . $row[1] . ' ringtimer 0"');
exec('asterisk -rx "database put AMPUSER/' . $row[1] . ' voicemail ' . $vm . '"');
exec('asterisk -rx "database put CW ' . $row[1] . ' ENABLED"');
 
if ($row[2] != '') {
exec('asterisk -rx "database put AMPUSER/' . $row[1] . ' cidname ' . $name . '"');
}
 
if ($row[3] != '') {
exec('asterisk -rx "database put AMPUSER/' . $row[1] . ' outboundcid ' . $row[3] . '"');
}
 
// If Follow Me is specified
if ($row[8] == "1") {
 
if (strlen($row[9]) == 11) {
$num = "-" . $row[9] . "#";
} else {
$num = "";
}
 
if ($row[11]) {
$time = $row[11];
} else {
$time = '';
}
 
//echo "FollowMe Enabled!\n";
$fmedel = 'DELETE FROM findmefollow WHERE grpnum = "' . $row[1] . '"';
$fmeins = 'INSERT INTO findmefollow (grpnum, strategy, grptime, grppre, grplist, annmsg_id, postdest, dring, needsconf, remotealert_id, toolate_id, ringing, pre_ring) VALUES ("' . $row[1] . '", "ringallv2", "' . $time . '", "", "' . $row[1] . $num . '", "", "ext-local,' . $row[1] . ',dest", "", "", "", "", "Ring", "0")';
 
$conn->exec($fmedel);
$conn->exec($fmeins);
 
exec('asterisk -rx "database put AMPUSER/' . $row[1] . '/followme changecid default"');
exec('asterisk -rx "database put AMPUSER/' . $row[1] . '/followme ddial DIRECT"');
exec('asterisk -rx "database put AMPUSER/' . $row[1] . '/followme grpconf DISABLED"');
exec('asterisk -rx "database put AMPUSER/' . $row[1] . '/followme grplist ' . $row[1] . $num . '"');
exec('asterisk -rx "database put AMPUSER/' . $row[1] . '/followme grptime ' . $row[11] . '"');
exec('asterisk -rx "database put AMPUSER/' . $row[1] . '/followme prering 0"');
}
}
 
// If there's an error, display display it and then exit.
} catch(PDOException $e) {
//$conn->rollback();
echo "Error: " . $e->getMessage() . "\n";
exit(1);
}
 
 
// Close the connection to the database/
$conn = null;
 
 
// Test for and add some generic AstDB stuff if it's not there.
$cc_agent_policy = shell_exec('asterisk -rx "database get AMPUSER//ccss cc_agent_policy"');
$cc_offer_timer = shell_exec('asterisk -rx "database get AMPUSER//ccss cc_offer_timer"');
$qnostate = shell_exec('asterisk -rx "database get AMPUSER//queues qnostate"');
 
if ($cc_agent_policy == "Database entry not found.\n" || $cc_agent_policy == "Value: \n") {
exec('asterisk -rx "database put AMPUSER//ccss cc_agent_policy generic"');
}
 
if ($cc_offer_timer == "Database entry not found.\n" || $cc_offer_timer == "Value: \n") {
exec('asterisk -rx "database put AMPUSER//ccss cc_offer_timer 30"');
}
 
if ($qnostate == "Database entry not found.\n" || $qnostate == "Value: \n") {
exec('asterisk -rx "database put AMPUSER//queues qnostate usestate"');
}
 
 
// Reload Asterisk. (Same as clicking the "Apply Config" button)
exec("/var/lib/asterisk/bin/retrieve_conf");
exec("/var/lib/asterisk/bin/module_admin reload");