Phonebook store location

I’m looking for the location where Asterisk Phonebook stores its data. Can’t seem to find the data in the MySQL database on the server, so I guess it must be somewhere else?

Try /var/lib/asterisk/astdb.sqlite3

You’re welcome.

At the bash prompt:

asterisk -x "database show cidname"

To get a name associated with a specific number:

asterisk -x "database get cidname xxxxxxxxxx"

That works! Out of the box FreePBX’s PHP intallation apparently doesn’t support SQLite3, installed that and now I can get to work!

Thanks!!!

This script works for me, hopefully it will help somebody in the future:

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

$db = new SQLite3('/var/lib/asterisk/astdb.sqlite3');

$results = $db->query('SELECT * FROM astdb;');

while ($row = $results->fetchArray()) {
    //find
    $find='/cidname/';
   
    if(strpos($row['key'], $find)!==false) {
        echo '<DirectoryEntry>';
            echo '<Name>'.htmlspecialchars($row['value']).'</Name>';
            echo '<Telephone>'.str_replace($find, '', $row['key']).'</Telephone>';
        echo '</DirectoryEntry>';
    }
}

echo '</YeastarIPPhoneDirectory>';
?>

First run the following command to install SQLite3: apt-get install php5-sqlite

3 Likes

Cool. That might come in handy. Thanks.

that did the trick!
Thanks

I hate to revive an old discussion but I just came across this and it looks like it will work for me too. Just wondering where on the server you put this file and how you get the phones to read the script. Right now I am using Yealink T46s phones with a Sangoma UC 60 PBX box.

The second part is to install the script someplace that the phones can “see” it. In your phone config there should be a “directory URL” or “directory XML”. The script generates and “displays” that XML for the phone to consume.

So, for example, assuming your phone server is on the Local LAN and at 192.168.0.1, if your phone can see “http://192.168.0.1/directory/phonedir.php”, you can store it there and let the webserver run it. It needs to be someplace that will interpret the PHP code. On the phone server, that will probably be in a folder called “/var/www/html/directory/”.

please , help me
i have elastix so i want all cisco voip taking their personal direcoty from it
could that code help me , and where i put the code ???

Nope - Cisco uses a different XML Directory format. Other than that, yeah, it would be a good start.

could you tell me where i could put the XML command when i enter root to elastix ?
what should i type t?
thanx for your replying i realy need your help

Hi, I’m not a pro on this but you can download WinSCP and upload the files with it. actually it is very useful while you need to work with files on linux platforms and you use windows client.

Actually I have an script to use with cisco phone which really works. but just for internal extensions, ring groups and personal (does not work).
I’m trying to combine the script @vespino gave us and mine.
here is the script:

<?php
header ("content-type: text/xml");
$xtn = array_key_exists('xtn', $_GET) ? $_GET['xtn'] : NULL;
$mode = array_key_exists('mode', $_GET) ? $_GET['mode'] : NULL;
$url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
if (!@include_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) {
    include_once('/etc/asterisk/freepbx.conf');
}
// -----
switch ($mode) {
    case "extensions":
        echo directoryShow("extensions", $url, $xtn);
    break;
    case "groups":
        echo directoryShow("groups", $url, $xtn);
    break;
    case "personal":
        echo directoryShow("personal", $url, $xtn);
    break;
    default:
        echo directoryMenu($url, $xtn);
    }
// -----
function directoryMenu($url, $xtn) {
    $menudata = array(
        array('Internal Phonebook', "$url?mode=extensions")
        ,array('Ring groups', "$url?mode=groups")
        ,array('Personal contacts', "$url?mode=personal&amp;xtn=$xtn")
    );
    $xml = new SimpleXMLElement('<CiscoIPPhoneMenu/>');
    $xml -> addChild('Prompt', 'Select a directory');
 
    foreach ($menudata as $menuitem) {
        $menuItem = $xml -> addChild('MenuItem');
        $menuItem -> addChild('Name', $menuitem[0]);
        $menuItem -> addChild('URL', $menuitem[1]);
    }
    return ($xml->asXML());
}
// -----
function directoryShow($mode, $url, $xtn) {
    global $db;
    switch ($mode) {
        case "extensions":
            $sql = "SELECT name, extension FROM users WHERE name NOT LIKE '%FAX%' AND extension < 400 ORDER BY name";
            $title = "Internal directory";
            $prompt = "Select a name";
        break;
        case "groups":
            $sql = "SELECT description, grpnum FROM ringgroups WHERE grpnum < 599 ORDER BY description";
            $title = "Department directory";
            $prompt = "Select a group";
        break;
        case "personal":
            $sql = "
                SELECT
                    CONCAT(contactmanager_group_entries.displayname, ' (', contactmanager_entry_numbers.type,')') AS 'name'
                    ,contactmanager_entry_numbers.number AS 'extension'
                FROM contactmanager_groups 
                LEFT JOIN contactmanager_group_entries ON contactmanager_groups.id = contactmanager_group_entries.groupid
                LEFT JOIN contactmanager_entry_numbers ON contactmanager_group_entries.id = contactmanager_entry_numbers.entryid 
                WHERE contactmanager_groups.name = '$xtn'
                ORDER BY contactmanager_group_entries.displayname
            ";
            $title = "Personal contacts";
            $prompt = "Select a name";
        break;
        default:
        return;
    }
 
    $results = $db->getAll($sql, DB_FETCHMODE_ORDERED);
    $numrows = count($results);
    $endoflist = false;
    $xml = new SimpleXMLElement('<CiscoIPPhoneDirectory/>');
    $xml -> addChild('Title', $title);
    $xml -> addChild('Prompt', $prompt);
 
    $page = array_key_exists('page', $_GET) ? $_GET['page'] : 0;
    $count = $page * 32;
 
    for ($row=$count; $row <= $count+31; $row++) {
        if (is_null($results[$row][0])) {
            $endoflist = true;
        } else {
            $endoflist = false;
            $directoryEntry = $xml -> addChild('DirectoryEntry');
            $directoryEntry -> addChild('Name', $results[$row][0]);
            $directoryEntry -> addChild('Telephone', $results[$row][1]);
        }
    }
    $softkey = $xml -> addChild('SoftKeyItem');
    $softkey -> addChild('Name', 'Exit');
    $softkey -> addChild('URL', 'SoftKey:Exit');
    $softkey -> addChild('Position', '1');
 
    $softkey = $xml -> addChild('SoftKeyItem');
    $softkey -> addChild('Name', 'Dial');
    $softkey -> addChild('URL', 'SoftKey:Dial');
    $softkey -> addChild('Position', '2');
 
    if ($page > 0) {
        $softkey = $xml -> addChild('SoftKeyItem');
        $softkey -> addChild('Name', 'Prev');
        $softkey -> addChild('URL', 'SoftKey:Exit');
        $softkey -> addChild('Position', '3'); 
    }
 
    if (!$endoflist) {
        $softkey = $xml -> addChild('SoftKeyItem');
        $softkey -> addChild('Name', "Next");
        $softkey -> addChild('URL', "$url?mode=$mode&amp;page=".++$page);
        $softkey -> addChild('Position', '4');
    }
    return ($xml->asXML()); 
}
?>

thanx for your time , i will read the script hope it will help .

I am not sure if there is a difference in PBX versions and how this works but when I try to run this code It returns with an Fatal error saying “Call to a member function fetchArray() on Boolean”. I think this means the array is being returned as FALSE. Any help is greatly appreciated.

Hello everyone…

And here is almost the end of 2017 and I have a problem with pulling the phonebook into the phones. I have Htek phones, 14 of them and FPBX 13. Guys, can You tell me… ? Where should I put this script? Where is the exact location of the “phonebook.xml” ?I am not sure I understood these posts completely. Can someone help?

Best Regards.

Hello again…

Ok, I have managed to make it work but this syncs only asterisk phonebook contacts. What about the extensions?

Best Regards.

This should be added to FreePBX as a simple shared phonebook solution.

Action breads action, submit a feature request.