Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [php]
- <?php
- // Description: Generating a XML Phonebook and TXT Directory file from FreePBX MySQL DB
- // Modification: Use 'users' table instead of devices allowing exclusion of some devices
- // Suggested Use: setup a cronjob to run this periodically to keep it up to date
- // Original Author: Yves Menge (yves.menge@gmail.com)
- // Script modified in 2016 by someone other than Yves Menge
- // XML phonebook config confirmed to work with Grandstream 1450/2120/2140/2160 phones
- //!!Enable for Debug only!!
- // error_reporting(E_ALL);
- // ini_set("display_errors", 'ON');
- // Database settings
- $DBhost = ""; //** Insert your host here (ie: localhost)
- $DBuser = ""; //** Insert your DB user here
- $DBpass = ""; //** Insert your password here
- $DBdatabase = "asterisk"; //** change only when installed Free PBX in a non-common way!
- // 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";
- //$DBquery = "SHOW COLUMNS FROM users";
- $QUERYresult = mysql_query($DBquery, $DBlink) or die("Data not found.");
- //Write Directory File
- $myfile = fopen('/var/www/html/SOMEPLACE/DirectoryFile.txt', "w") or die("Unable to open file");
- //Setup XMLWriter
- $writer = new XMLWriter();
- $writer->openURI('/var/www/html/SOMEPLACE/phonebook.xml'); //** If your TFTP server is using another root
- directory as /tfptboot, change the path here!
- $writer->setIndent(4);
- //Begin output
- $writer->startDocument('1.0');
- $writer->startElement('AddressBook');
- //$count = 0;
- //Add extensions / contacts from devices to the xml phonebook
- while ($contact=mysql_fetch_array($QUERYresult))
- {
- if(preg_match('/\s/',$contact[1]))
- {
- //$count++;
- fwrite($myfile, $contact[0]." ".$contact[1]."\n");
- list($ln,$fn) = explode(' ',$contact[1],2); //** Separate first name by space. Last name = remainder of
- field
- //!!Enable for Debug only!!
- // print $fn . $ln . $contact['extension'] . '<br>';
- $writer->startElement('Contact');
- $writer->writeElement('LastName',$ln);
- $writer->writeElement('FirstName',$fn);
- $writer->startElement('Phone');
- $writer->writeElement('phonenumber',$contact['extension']);
- $writer->writeElement('accountindex','0');
- $writer->endElement();
- $writer->endElement();
- }
- }
- //print "Count: ".$count;
- fclose($myfile);
- $writer->endElement();
- $writer->endDocument();
- $writer->flush();
- ?>
- [/php]
Add Comment
Please, Sign In to add comment