Display Asterisk database CID records on Cisco 7940/60 IP phones

Display Asterisk database CID records on Cisco 7940/60 IP phones

This is a little PHP script to display the internal caller id database of an asterisk as phonebook on a Cisco IP phone.

<?php
  #############################################################################
  #                                                                           #
  # directory.php                                                             #
  #                                                                           #
  # Display Asterisk database CID records on Cisco 7940/60 IP phones          #
  #                                                                           #
  # Please store all external nos w/ full prefix/areacode                     #
  #                                                                           #
  # (w) 2005 by Stefan Gofferje, License: GPL                                 #
  #                                                                           #
  # Inspired by Asterisk CID scripts by Philipp von Klitzing                  #
  #                                                                           #
  # Don't forget to modify /etc/sudoers:                                      #
  #  wwwrun  ALL=(ALL)       NOPASSWD:   /usr/sbin/asterisk                   #
  #                                                                           #
  #############################################################################
  #                                                                           #
  # If _AKZ is defined, any no. in the db beginning with _NATIONAL will       #
  # be prefixed with _AKZ in the phone directory.                             #
  
  define (_AKZ,"0");      #Prefix to dial an external no.
                          #[de] Amtskennziffer
  define (_NATIONAL,"0"); #Prefix for national calls

  #                                                                           #
  #############################################################################
  
  header("Content-type: text/xml");
  header("Connection: close");
  header("Expires: -1");
?>

<CiscoIPPhoneDirectory>
<Title>Asterisk CID Directory</Title>
<Prompt>Please Select</Prompt>

<?php
  set_time_limit(5);
  exec("sudo /usr/sbin/asterisk -rx "database show cidname"",$var);

  for ($i=0;$i< sizeof($var);$i++) {
    if (preg_match("/^/cidname/(d+)[[:space:]]+:[[:space:]]+(.+)/",$var[$i],$res)) {
      $entry[$i][name]=$res[2];
      $entry[$i][no]=$res[1];
      if (defined("_AKZ")) if (strncmp($entry[$i][no],_NATIONAL,1)==0) $entry[$i][no]=_AKZ.$entry[$i][no];
    }
  }
  sort($entry);
  for ($i=0;$i< sizeof($entry);$i++) {
    print "<DirectoryEntry>n";
    print "<Name>".utf8_encode($entry[$i][name])."</Name>n";
    print "<Telephone>".$entry[$i][no]."</Telephone>n";
    print "</DirectoryEntry>n";
  }
?>

<SoftKeyItem>
  <Name>Dial</Name>
  <URL>SoftKey:Dial</URL>
  <Position>1</Position>
  </SoftKeyItem>
<SoftKeyItem>
  <Name>Return</Name>
  <URL>SoftKey:Exit</URL>
  <Position>3</Position>
</SoftKeyItem>
</CiscoIPPhoneDirectory>