<?php
/************************************************************************/
/* config.php                                                           */
/*----------------------------------------------------------------------*/
/* Defines Language, GetConfiguration, Configuration.                   */
/*----------------------------------------------------------------------*/
/* Copyright (c) 2008-2009 Avaya Inc.                                   */
/************************************************************************/


class Language
{
  public  $name;
  public  $code;
  function __construct($name, $code)
  {
    $this->name = $name;
    $this->code = $code;
  }
}


function GetConfiguration()
{
  static $configuration = null;
  if( !isset( $configuration ) )
  {
    $configuration = new Configuration();
  }
  return $configuration;
}

class Configuration
{

  //it could be accessed from webservice dir or scripts
  //when from webservice it tries to look in webservice dir not in the
  //in the path where config.php lives....
  private $configFile = 'webvoicemail.xml';
  private $items = array();
  private $languages = array();

  function __construct()
  {
    $this->Parse();
  }

  function __get($id)
  {
  	if( array_key_exists( $id, $this->items ) )
  	{
  		return $this->items[ $id ];
  	}
  	else
  	{
  		return NULL;
  	}
  }
  function __set($id,$v) { $this->items[ $id ] = $v; }

  function Save()
  {
    $doc = new DOMDocument();
    $doc->formatOutput = true;

    $r = $doc->createElement( 'config' );
    $doc->appendChild( $r );

    foreach( $this->items as $k => $v )
    {
      $kn = $doc->createElement( $k );
      $kn->appendChild( $doc->createTextNode( $v ) );
      $r->appendChild( $kn );
    }

    copy( $this->configFile, $this->configFile.'.bak' );

    $doc->Save( $this->configFile );
  }

  function Parse()
  {
    $doc = new DOMDocument();
    $doc->load( $this->configFile );

    $cn = $doc->getElementsByTagName( 'config' );

    $nodes = $cn->item(0)->getElementsByTagName( '*' );
    foreach( $nodes as $node )
    {
      $this->items[ $node->nodeName ] = $node->nodeValue;
    }
  }
}

?>
