irimport

#!/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: irimport [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) {
 
// We need to run a total of four mysql commands. The first three are easy:
$ciddel = "DELETE FROM cidlookup_incoming WHERE extension = '" . $row[0] . "' AND cidnum = ''";
$cidins = 'INSERT INTO cidlookup_incoming (cidlookup_id, extension, cidnum) VALUES ("0", "' . $row[0] . '", "")';
$diddel = "DELETE FROM incoming WHERE cidnum = '' AND extension = '" . $row[0] . "'";
 
// For the fourth command, we need to determine a destination string.
if ($row[2] == "terminate") {
$dest = "app-blackhole," . $row[3] . ",1";
} elseif ($row[2] == "extension") {
$dest = "from-did-direct," . $row[3] . ",1";
} elseif ($row[2] == "ivr") {
$dest = "ivr-" . $row[3] . ",s,1";
} elseif ($row[2] == "timecondition") {
$dest = "timeconditions," . $row[3] . ",1";
} elseif ($row[2] == "ringgroup") {
$dest = "ext-group," . $row[3] . ",1";
} else {
echo "Error: " . $row[0] . " NOT imported successfully.\n";
continue;
}
 
// Save the fourth command using the above destination string.
$didins = "INSERT INTO incoming (cidnum,extension,destination,privacyman,pmmaxretries,pmminlength,alertinfo, ringing, mohclass, description, grppre, delay_answer, pricid) values ('','" . $row[0] . "','" . $dest . "','0','3','10','', '', 'default', '" . $row[1] . "', '', '', '')";
 
// Execute all four mysql commands and let us know if they were successful.
$conn->exec($ciddel);
$conn->exec($cidins);
$conn->exec($diddel);
$conn->exec($didins);
echo $row[0] . " imported successfully.\n";
}
 
// If there's an error, display display it and then exit.
} catch(PDOException $e) {
echo "Error: " . $e->getMessage() . "\n";
exit(1);
}
 
 
// Close the connection to the database/
$conn = null;
 
 
// Reload Asterisk. (Same as clicking the "Apply Config" button)
exec("/var/lib/asterisk/bin/retrieve_conf");
exec("/var/lib/asterisk/bin/module_admin reload");