<?php
$DBhost = "localhost"; // Insert your host here
$DBuser = "root"; // Insert your DB user here
$DBpass = ""; // Insert your password here
$DBdatabase = "asterisk"; // change only when installed Free PBX in a non-common way!
mysql_query ("set_client='utf8'");
mysql_query ("set character_set_results='utf8'");
mysql_query ("set collation_connection='utf8_general_ci'");
mysql_query ("SET NAMES utf8");
mysql_set_charset( 'utf8' );
// Connect to the Database and get all users
$DBlink = mysql_connect($DBhost, $DBuser, $DBpass) or die("Could not connect to host.");
mysql_select_db($DBdatabase, $DBlink) or die("Could not find database.");
$DBquery = "SELECT extension, name FROM users ORDER BY name ASC";
$QUERYresult = mysql_query($DBquery, $DBlink) or die("Data not found.");
require_once("/etc/freepbx.conf");
$mysqli = new mysqli($amp_conf['AMPDBHOST'], $amp_conf['AMPDBUSER'], $amp_conf['AMPDBPASS'], $amp_conf['AMPDBNAME']);
function DBQuery($query){
global $mysqli;
if (!$sqlResult = mysqli_query($mysqli, $query)) {
trigger_error('DB query failed: ' . $mysqli->error . "\nquery: " . $query);
return false;
} else {
$all_rows = array();
while ($row = mysqli_fetch_assoc($sqlResult)) {
$all_rows[] = $row;
}
return $all_rows;
}
}
function formatXML($xml){
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xml);
$dom->formatOutput = TRUE;
return $dom->saveXml();
}
function httpAuthenticate(){
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo '401 Unauthorized';
exit;
}
if (!isset($_SERVER['PHP_AUTH_USER'])) {
httpAuthenticate();
} else {
$PHP_AUTH_USER = mysqli_real_escape_string($mysqli, $_SERVER['PHP_AUTH_USER']);
$userPasswordLookupResult = DBQuery("select * from sip where id='$PHP_AUTH_USER' and keyword='secret'");
if (!$userPasswordLookupResult || !$userPasswordLookupResult[0]['data'] == $_SERVER['PHP_AUTH_PW']) {
httpAuthenticate();
}
}
header('Content-type: application/xml; charset=utf-8');
$xml_obj = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><AddressBook/>');
foreach (DBQuery("SELECT extension, name FROM users ORDER BY name ASC") as $contact){
list($fn,$ln) = explode(" ", $contact['name'],2);
$Contact = $xml_obj->addChild('Contact');
$FirstName = $Contact->addChild('FirstName',$fn);
$LastName = $Contact->addChild('LastName', $ln);
$Phone = $Contact->addChild('Phone');
$phonenumber = $Phone->addChild('phonenumber', $contact['extension']);
$accountindex = $Phone->addChild('accountindex', 1);
}
print formatXML($xml_obj->asXML());
?>