Script for contacts on Yealink no longer works on FreePBX 15 (worked on FreePBX14)

Here is a script that I’ve been using for a while to populate the remote address book on yealink phones. I save it as contacts.php in the /var/html/www/ directory:

<?php
/*
The purpose of this file is to read all the Contact Manager entries for the specified group
and then output them in a Yealink Remote Address Book formatted XML syntax.
Instructions on how to use can be found here:
https://mangolassi.it/topic/18647/freepbx-contact-manager-to-yealink-address-book
*/
// Edit this varibale to match the name of hte group in Contact Manager
$contact_manager_group = "Contacts";
header("Content-Type: text/xml");
// get the MySQL/MariaDB login information from the amportal configuration file.
define("AMP_CONF", "/etc/amportal.conf");
$file = file(AMP_CONF);
if (is_array($file)) {
    foreach ($file as $line) {
        if (preg_match("/^\s*([a-zA-Z0-9_]+)=([a-zA-Z0-9 .&[email protected]=_!<>\"\']+)\s*$/",$line,$matches)) {
            $amp_conf[ $matches[1] ] = $matches[2];
        }
    }
}
require_once('DB.php'); //PEAR must be installed
$db_user = $amp_conf["AMPDBUSER"];
$db_pass = $amp_conf["AMPDBPASS"];
$db_host = $amp_conf["AMPDBHOST"];
$db_name = $amp_conf["AMPDBNAME"];
$datasource = 'mysql://'.$db_user.':'.$db_pass.'@'.$db_host.'/'.$db_name;
$db = DB::connect($datasource); // attempt connection
$type="getAll";
// This pulls every number in contact maanger that is part of the group specified by $contact_manager_group
$results = $db->$type("SELECT cen.number, cge.displayname FROM contactmanager_group_entries AS cge LEFT JOIN contactmanager_entry_numbers AS cen ON cen.entryid = cge.id WHERE cge.groupid = (SELECT cg.id FROM contactmanager_groups AS cg WHERE cg.name = '$contact_manager_group');", null);
//dump the result into an array.
foreach($results as $result){
    $extensions[] = array($result[0],$result[1]);
}
// output the XML header info
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
// Output the XML root. This tag must be in the format XXXIPPhoneDirectory
// You may change the word Company below, but no other part of the root tag.
echo "<CompanyIPPhoneDirectory  clearlight=\"true\">\n";
$index = 0;
if (isset($extensions)) {
    // Loop through the results and output them correctly.
    // Spacing is setup below in case you wish to look at the result in a browser.
    foreach ($extensions as $key=>$extension) {
        $index= $index + 1;
        echo "    <DirectoryEntry>\n";
        echo "        <Name>$extension[1]</Name>\n";
        echo "        <Telephone>$extension[0]</Telephone>\n";
        echo "    </DirectoryEntry>\n";
    }
}
// Output the closing tag of the root. If you changed it above, make sure you change it here.
echo "</CompanyIPPhoneDirectory>\n";
?>

It was working fine until I tried to use it on FreePBX 15 for the first time. The result is the same on 2 different system, an XML page with the same data on both systems. The following entries are done on some lines “B” D" “E” “S” “Array” “Array”

In order for the script to work, I have to install PHP-Pear by using the command “yum install -y php-pear-DB”

I’m trying to find out what is wrong with the script but I can’t figure it out (I’m not a very good programmer). Also, I think that the script works as intented since there is an XML array created when I browse the contacts.php page manually. The data “B” D" “E” “S” “Array” “Array” must be coming from somewhere

PEAR has not been used since 12 I believe so it was finally removed…
Below is off the top of my head and likely has errors…
see https://wiki.freepbx.org/display/FOP/PDO+Cheatsheet

<?php
include '/etc/freepbx.conf';
$db = FreePBX::Database();
$stmt = $db->prepare("SELECT cen.number, cge.displayname FROM contactmanager_group_entries AS cge LEFT JOIN contactmanager_entry_numbers AS cen ON cen.entryid = cge.id WHERE cge.groupid = (SELECT cg.id FROM contactmanager_groups AS cg WHERE cg.name = :cg)");
$stmt->execute(array(':cg' => $contact_manager_group));
$items =  $stmt->fetchAll(\PDO::FETCH_ASSOC);

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.