Where does FreePBX save external contacts?

Hi everyone,
Does anyone know where the external contacts added in the contact manager are saved? I’ve configured an XML script to extract the contacts, but I can only see the internal ones. I can’t figure out in which database they are saved.

Can anyone help me?

Thanks.

In the MySQL Database, Asterisk > contactmanager_entry_numbers (numbers) and contactmanager_group_entries (name and other infos)

PHP scripts you can get some ideas from
lgaetz-cmcheck.php · GitHub
lgaetz-cmadd.php · GitHub

2 Likes
1 Like

Thank you for the response and for indicating which database it relies on

I have resolved and completed the script to read the contact manager for both internal contacts using SQLite3 and external contacts relying on MySQL. To read the MySQL DB, I added a user with read and write access and adapted the script based on the responses obtained. Now, on Yealink IP phones, I can see all the contacts under the remote address book. However, I can’t get it to display on Sangoma IP phones. There might be an issue with the auto provisioning, but I’m not sure.

1 Like

I attached my configuration:

<?php header("Content-type: text/xml"); echo '<?xml version="1.0" encoding="UTF-8"?>';

echo ‘’;

try {
$db = new SQLite3(‘/var/lib/asterisk/astdb.sqlite3’);

// Internal contacts
echo '<!-- Debug: Start fetching internal contacts -->';
$internalResults = $db->query('SELECT * FROM astdb WHERE key LIKE "/AMPUSER/%/cidname";');
while ($internalResults && $row = $internalResults->fetchArray(SQLITE3_ASSOC)) {
    $cidnum = str_replace('/AMPUSER/', '', str_replace('/cidname', '', $row['key']));
    $cidname = $row['value'];
    echo '<DirectoryEntry>';
    echo '<Name>'.htmlspecialchars($cidname).'</Name>';
    echo '<Telephone>'.htmlspecialchars($cidnum).'</Telephone>';
    echo '</DirectoryEntry>';
}
echo '<!-- Debug: Finished fetching internal contacts -->';

// External contacts from MySQL
echo '<!-- Debug: Start connecting to MySQL -->';
$mysqli = new mysqli("localhost", "utente", "password", "asterisk");

if ($mysqli->connect_error) {
    throw new Exception("Connection failed: " . $mysqli->connect_error);
}
echo '<!-- Debug: Successfully connected to MySQL -->';

// Use the groupid '2' for the "family" group
$groupid = 2;

echo '<!-- Debug: Start fetching external contacts -->';
$externalResults = $mysqli->query("
    SELECT
        contactmanager_entry_numbers.number,
        contactmanager_group_entries.displayname
    FROM
        contactmanager_entry_numbers
    JOIN
        contactmanager_group_entries
    ON
        contactmanager_entry_numbers.entryid = contactmanager_group_entries.id
    WHERE
        contactmanager_group_entries.groupid = $groupid
");

if ($externalResults === false) {
    throw new Exception("Query failed: " . $mysqli->error);
}

if ($externalResults->num_rows == 0) {
    echo '<Error>No external contacts found</Error>';
} else {
    while ($row = $externalResults->fetch_assoc()) {
        $name = $row['displayname'];
        $number = $row['number'];

        echo '<DirectoryEntry>';
        echo '<Name>'.htmlspecialchars($name).'</Name>';
        echo '<Telephone>'.htmlspecialchars($number).'</Telephone>';
        echo '</DirectoryEntry>';
    }
}
echo '<!-- Debug: Finished fetching external contacts -->';

$mysqli->close();
$db->close();

} catch (Exception $e) {
echo ‘’ . htmlspecialchars($e->getMessage()) . ‘’;
}

echo ‘’;
?>


NB:
You need to remember to change the username and password of your PBX as in the example: $mysqli = new mysqli("localhost", "user", "password", "asterisk"); and modify the name of the group created in the external contacts section and its groupid (which can be found by looking at the MySQL tables), e.g.: // Use the groupid '2' for the "family" group.

Take a look at @lgaetz answer here: Where does FreePBX save external contacts? - #3 by lgaetz and use the FreePBX PHP API to do the work. Please don’t directly query the astdb sqlite (unless you want to lock it up I guess?) or the mysql…

By the way you don’t even need the MySQL password if you use the FreePBX PHP API.

1 Like