电话簿XML文件(Users_Config.xml)
<?xml version="1.0"?>
<Config>
<RTXUser name="ZhangY">
<Host>192.168.10.57</Host>
<User>3813</User>
<Pass>3813</Pass>
<RPort>49200</RPort>
<Status>0</Status>
</RTXUser>
<RTXUser name="Xb">
<Host>127.0.0.7</Host>
<User>320825</User>
<Pass>320826</Pass>
<RPort>99233</RPort>
<Status>0</Status>
</RTXUser>
<RTXUser name="test1">
<Host>127.0.0.1</Host>
<User>3208</User>
<Pass>3208</Pass>
<RPort>99233</RPort>
</RTXUser>
</Config>
集合类(Collection.php)
<?php
/**
* @author yandaomin
* @copyright 2010
*/
//集合类 类似于C#中的List
class Collection implements IteratorAggregate
{
// $items is the list that collects objects
protected $items = array();
// $onload is for the callback function
// $isLoaded is the flag indicates whether the callback function has invoked
protected $onLoad;
protected $isLoaded = false;
public function getIterator()
{
return new CollectionIterator($this);
}
public function addItem($item, $key = null)
{
$this->checkCallback();
if (!is_null($key))
{
if (!isset($this->items[$key]))
{
$this->items[$key] = $item;
}
else
{
throw new Exception('The key already exists.');
}
}
else
{
$this->items[] = $item;
}
}
public function addRange($items)
{
foreach ($items as $item)
{
$this->addItem($item);
}
}
public function clear()
{
$items = array();
}
public function contains($item)
{
foreach ($this->items as $temp)
{
if ( $item == $temp)
return true;
}
return false;
}
public function remove($key)
{
$this->checkCallback();
if (isset($this->items[$key]))
{
unset($this->items[$key]);
}
else
{
throw new Exception('Incorrect key.');
}
}
public function removeAt($index)
{
$keys = array_keys($this->items);
$currentKey = $keys[$index];
return $this->remove($currentKey);
}
public function getCollection()
{
return $this->items;
}
public function getItem($key)
{
$this->checkCallback();
if (isset($this->items[$key]))
{
return $this->items[$key];
}
else
{
throw new Exception('Incorrect key.');
}
}
public function getLength()
{
$this->checkCallback();
return sizeof($this->items);
}
public function getKeys()
{
$this->checkCallback();
return array_keys($this->items);
}
public function insert($index,$item)
{
$first_array = array_splice ($this->items, 0, $index);
$array = array_merge ($first_array, $item, $this->items);
}
public function isExist($key)
{
$this->checkCallback();
return isset($this->items[$key]);
}
public function isEmpty()
{
return (($this->size()==0)?1:0);
}
public function setCallback($functionName, $owner = null)
{
if (!is_null($owner))
{
$callback = array($owner, $functionName);
}
else
{
$callback = $functionName;
}
if (!is_callable($callback, false, $callableName))
{
throw new Exception('Invalid callback function.');
}
$this->onLoad = $callback;
}
protected function checkCallback()
{
if (isset($this->onLoad) && !$this->isLoaded)
{
$this->isLoaded = true;
call_user_func($this->onLoad, $this);
}
}
}
class CollectionIterator implements Iterator
{
protected $collection1;
protected $index;
protected $keys;
public function __construct(Collection $col)
{
$this->collection1 = $col;
$this->index = 0;
$this->keys = $col->getKeys();
}
public function rewind()
{
$this->index = 0;
}
public function valid()
{
return $this->index < $this->collection->getLength();
}
public function key()
{
return $this->keys[$this->index];
}
public function current()
{
return $this->collection->getItem($this->keys[$this->index]);
}
public function next()
{
$this->index++;
}
}
?>
电话簿管理类(TelBookManager.php)
<?php
/**
* @author yandaomin
* @copyright 2010
*/
//引用集合类
include_once("./Collection.php");
//电话簿条目类(OM对象)
class TelBook
{
public $name;//人名,关键字,此字段不可改
public $host;//主机名,SIPServer IP地址
public $user;//用户名,一般为分机号码
public $pass;//密码
public $rport;//RTP端口
}
//电话簿管理类
class TelBookManager
{
//电话簿XML文件名
private $_xmlFileName;
//电话簿条目集合
private $Items;
//刷新函数,当你做了修改后,请使用该函数进行刷新以获取最新的电话簿条目集合
private function _fresh()
{
$this->Items = new Collection();
$doc = new DOMDocument();
$doc->load($this->_xmlFileName);
$users = $doc->getElementsByTagName("RTXUser");
foreach($users as $user1)
{
$tempTelBook = new TelBook();
$tempTelBook->name = $user1->getAttribute("name");
$tempTelBook->host = $user1->getElementsByTagName("Host")->item(0)->nodeValue;
$tempTelBook->user = $user1->getElementsByTagName("User")->item(0)->nodeValue;
$tempTelBook->pass = $user1->getElementsByTagName("Pass")->item(0)->nodeValue;
$tempTelBook->rport = $user1->getElementsByTagName("RPort")->item(0)->nodeValue;
$this->Items->addItem($tempTelBook, $tempTelBook->name);
}
}
//构造函数,当输入XML文件夹名会自动加载所有电话簿条目
public function __construct($aFileName = null)
{
$this->_xmlFileName = $aFileName;
$this->_fresh();
}
public function set_XMLFileName($fileName)
{
$this->_xmlFileName = $fileName;
}
//获取电话簿条目集合类型是Collection
public function Get_AllItem()
{
return $this->Items->getCollection();
}
//根据人名检查电话簿条目是否存在,
private function Check_Existed($Item)
{
return $this->Items->isExist($Item->name);
}
private function _Add_Item($Item)
{
$doc = new DOMDocument();
$doc->load($this->_xmlFileName);
$Config = $doc->getElementsByTagName("Config");
$Config = $Config->item(0);
$user2 = $doc->createElement( "RTXUser" );
$nameProp = $doc->createAttribute("name");
$nameProp->appendChild($doc->createTextNode($Item->name));
$user2->appendChild($nameProp);
$temp = $doc->createElement("Host");
$temp->appendChild($doc->createTextNode($Item->host));
$user2->appendChild($temp);
$temp = $doc->createElement("User");
$temp->appendChild($doc->createTextNode($Item->user));
$user2->appendChild($temp);
$temp = $doc->createElement("Pass");
$temp->appendChild($doc->createTextNode($Item->pass));
$user2->appendChild($temp);
$temp = $doc->createElement("RPort");
$temp->appendChild($doc->createTextNode($Item->rport));
$user2->appendChild($temp);
$Config->appendChild($user2);
$doc->save($this->_xmlFileName);
}
增加电话簿条目
public function Add_Item($Item)
{
if ($this->Check_Existed($Item))
{
return 0;
}
//save to xmlfile
$this->_Add_Item($Item);
//save to collection
$this->Items->addItem($Item, $Item->name);
//return collection item count
return $this->Items->getLength();
}
private function _Del_Item($name)
{
$doc = new DOMDocument();
$doc->load($this->_xmlFileName);
$users = $doc->getElementsByTagName("RTXUser");
$config = $doc->getElementsByTagName("Config");
$config = $config->item(0);
foreach($users as $user1)
{
$var1 = $user1->getAttribute("name");
if($name == $var1)
{
$config->removeChild($user1);
break;
//$user1->de
}
}
$doc->save($this->_xmlFileName);
}
//根据人名删除电话簿条目
public function Del_Item($name)
{
$this->_Del_Item($name);
$this->Items->remove($name);
return $this->Items->getLength();
}
private function _Edit_Item($Item)
{
$doc = new DOMDocument();
$doc->load($this->_xmlFileName);
$users = $doc->getElementsByTagName("RTXUser");
foreach($users as $user1)
{
$name = $user1->getAttribute("name");
if($name == $Item->name)
{
$temp = $user1->getElementsByTagName("Host")->item(0);
$tmpNode = $doc->createElement("Host", $Item->host);
$user1->replaceChild($tmpNode, $temp);
$temp = $user1->getElementsByTagName("User")->item(0);
$tmpNode = $doc->createElement("User", $Item->user);
$user1->replaceChild($tmpNode, $temp);
$temp = $user1->getElementsByTagName("Pass")->item(0);
$tmpNode = $doc->createElement("Pass", $Item->pass);
$user1->replaceChild($tmpNode, $temp);
$temp = $user1->getElementsByTagName("RPort")->item(0);
$tmpNode = $doc->createElement("RPort", $Item->rport);
$user1->replaceChild($tmpNode, $temp);
break;
}
}
$doc->saveXML();
$doc->save($this->_xmlFileName);
}
//修改电话簿条目,人名因为是关键字所以不可修改
public function Edit_Item($Item)
{
$this->_Edit_Item($Item);
$this->_fresh();
//return $this->Items->getLength();
}
}
?>
执行时间管理类(Exe_Duration.php)
<?php
/**
* @author phpadmin
* @copyright 2010
*/
class Exe_Duration
{
private $StartTime = 0;
private $StopTime = 0;
private function get_microtime()
{
list($usec, $sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
public function begin()
{
$this->StartTime = $this->get_microtime();
}
public function end()
{
$this->StopTime = $this->get_microtime();
return round(($this->StopTime - $this->StartTime) * 1000, 1);
}
}
?>
引用电话簿管理类DEMO(index.php)
<html>
<body>
<?php
echo "</br> ****** test TelBookManager *******</br>";
include_once("TelBookManager.php");
include_once("Exe_Duration.php");
function GetAllItem()
{
echo "</br></br> ****** get all items ******</br>";
$aTelBookManager = new TelBookManager("Users_Config.xml");
$var1 = $aTelBookManager->Get_AllItem();
foreach($var1 as $value)
{
echo "name = " . $value->name . "</br>host = " . $value->host . "</br>user = " . $value->user
. "</br>pass = " . $value->pass . "</br>rport = " . $value->rport . "</br>";
}
}
$bExeDuration = new Exe_Duration();
$bExeDuration->begin();
$aExeDuration = new Exe_Duration();
$aExeDuration->begin();
GetAllItem();
echo "</br>GetAllItem exetime is " . $aExeDuration->end() . " micro-second.</br>";
function Add_Item()
{
echo "</br></br> ****** add item ****";
$var2 = new TelBook();
$var2->name = "test1";
$var2->host = "127.0.0.1";
$var2->user = "3208";
$var2->pass = "3208";
$var2->rport = "99233";
$aTelBookManager = new TelBookManager("Users_Config.xml");
$var3 = $aTelBookManager->Add_Item($var2);
if($var3 > 0)
{
echo "</br> item add success.</br>";
$var1 = $aTelBookManager->Get_AllItem();
foreach($var1 as $value)
{
echo "name = " . $value->name . "</br>host = " . $value->host . "</br>user = " . $value->user
. "</br>pass = " . $value->pass . "</br>rport = " . $value->rport . "</br>";
}
}
else
{
echo "</br> item add fail.</br>";
if($var3 == 0)
echo "</br>" . $var2->name . " item already existed</br>";
}
}
$aExeDuration->begin();
Add_Item();
echo "</br>Add_Item exetime is " . $aExeDuration->end() . " micro-second.</br>";
function Del_Item()
{
echo "</br> ****** delete item ******";
$aTelBookManager = new TelBookManager("Users_Config.xml");
$var3 = $aTelBookManager->Del_Item("test1");
if($var3>0)
{
echo "</br> delete test1 item success.</br>";
$var1 = $aTelBookManager->Get_AllItem();
foreach($var1 as $value)
{
echo "name = " . $value->name . "</br>host = " . $value->host . "</br>user = " . $value->user
. "</br>pass = " . $value->pass . "</br>rport = " . $value->rport . "</br>";
}
}
else
{
echo "</br> delete test1 item fail.</br>";
}
}
//Del_Item();
echo "</br> ****** edit item ******</br>";
function Edit_Item()
{
$aTelBookManager = new TelBookManager("Users_Config.xml");
$var2 = new TelBook();
$var2->name = "Xb";
$var2->host = "127.0.0.7";
$var2->user = "320825";
$var2->pass = "320826";
$var2->rport = "99233";
$aTelBookManager->Edit_Item($var2);
echo "</br> edit item success </br>";
$var1 = $aTelBookManager->Get_AllItem();
foreach($var1 as $value)
{
echo "name = " . $value->name . "</br>host = " . $value->host . "</br>user = " . $value->user
. "</br>pass = " . $value->pass . "</br>rport = " . $value->rport . "</br>";
}
}
$aExeDuration->begin();
Edit_Item();
echo "</br>Edit_Item exetime is " . $aExeDuration->end() . " micro-second.</br>";
echo "</br>all exetime is " . $bExeDuration->end() . " micro-second.</br>";
?>
</body>
</html>