Guest User

FreePBX Export Directory (TXT) and Phonebook (XML)

a guest
Jan 18th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.51 KB | None | 0 0
  1. [php]
  2. <?php
  3. // Description: Generating a XML Phonebook and TXT Directory file from FreePBX MySQL DB
  4. // Modification: Use 'users' table instead of devices allowing exclusion of some devices
  5. // Suggested Use: setup a cronjob to run this periodically to keep it up to date
  6. // Original Author: Yves Menge (yves.menge@gmail.com)
  7. // Script modified in 2016 by someone other than Yves Menge
  8. // XML phonebook config confirmed to work with Grandstream 1450/2120/2140/2160 phones
  9.  
  10. //!!Enable for Debug only!!
  11. // error_reporting(E_ALL);
  12. // ini_set("display_errors", 'ON');
  13.  
  14. // Database settings
  15. $DBhost = ""; //** Insert your host here (ie: localhost)
  16. $DBuser = ""; //** Insert your DB user here
  17. $DBpass = ""; //** Insert your password here
  18. $DBdatabase = "asterisk"; //** change only when installed Free PBX in a non-common way!
  19.  
  20. // Connect to the Database and get all users
  21. $DBlink = mysql_connect($DBhost, $DBuser, $DBpass) or die("Could not connect to host.");
  22. mysql_select_db($DBdatabase, $DBlink) or die("Could not find database.");
  23. $DBquery = "SELECT extension, name FROM users ORDER BY name ASC";
  24. //$DBquery = "SHOW COLUMNS FROM users";
  25. $QUERYresult = mysql_query($DBquery, $DBlink) or die("Data not found.");
  26.  
  27.  
  28. //Write Directory File
  29. $myfile = fopen('/var/www/html/SOMEPLACE/DirectoryFile.txt', "w") or die("Unable to open file");
  30.  
  31. //Setup XMLWriter
  32. $writer = new XMLWriter();
  33. $writer->openURI('/var/www/html/SOMEPLACE/phonebook.xml'); //** If your TFTP server is using another root
  34.  
  35. directory as /tfptboot, change the path here!
  36. $writer->setIndent(4);
  37.  
  38. //Begin output
  39. $writer->startDocument('1.0');
  40. $writer->startElement('AddressBook');
  41. //$count = 0;
  42. //Add extensions / contacts from devices to the xml phonebook
  43. while ($contact=mysql_fetch_array($QUERYresult))
  44. {
  45.  if(preg_match('/\s/',$contact[1]))
  46.  {
  47.   //$count++;
  48.   fwrite($myfile, $contact[0]." ".$contact[1]."\n");
  49.  
  50.   list($ln,$fn) = explode(' ',$contact[1],2); //** Separate first name by space.  Last name = remainder of
  51.  
  52. field
  53.   //!!Enable for Debug only!!
  54.   // print $fn . $ln . $contact['extension'] . '<br>';
  55.   $writer->startElement('Contact');
  56.   $writer->writeElement('LastName',$ln);
  57.   $writer->writeElement('FirstName',$fn);
  58.   $writer->startElement('Phone');
  59.   $writer->writeElement('phonenumber',$contact['extension']);
  60.   $writer->writeElement('accountindex','0');
  61.   $writer->endElement();
  62.   $writer->endElement();
  63.  }
  64. }
  65. //print "Count: ".$count;
  66. fclose($myfile);
  67. $writer->endElement();
  68. $writer->endDocument();
  69. $writer->flush();
  70. ?>
  71. [/php]
Add Comment
Please, Sign In to add comment