cid, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "           name=\"" . htmlspecialchars($this->name, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "           address=\"" . htmlspecialchars($this->address, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "           phone=\"" . htmlspecialchars($this->phone, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "           phone2=\"" . htmlspecialchars($this->phone2, ENT_QUOTES, "UTF-8") . "\"\n");
		fwrite($fp, "           email=\"" . htmlspecialchars($this->email, ENT_QUOTES, "UTF-8") . "\"\n");	
		fwrite($fp, "           url=\"" . htmlspecialchars($this->url, ENT_QUOTES, "UTF-8") . "\">\n");
		fwrite($fp, "  \n");
	}
	public function show()
	{
		echo "
\n";
		echo "\tcid: " . $this->cid . "
";
		echo "\tname: " . $this->name . "
";
		echo "\taddress: " . $this->address . "
";
		echo "\tphone: " . $this->phone . "
";
		if($this->phone2) echo "\tphone2: " . $this->phone2 . "
";
		echo "\temail: email . "\">" . $this->email . "
";
		echo "\turl: url . "\">" . $this->url . "
";
		echo "
\n";
	}
	public function showshort()
	{
		echo "";
	}
	function Contact($cid,
									 $name,
									 $address,
									 $phone,
									 $phone2,
									 $email,
									 $url)
	{
		$this->cid = $cid;
		$this->name = $name;
		$this->address = $address;
		$this->phone = $phone;
		$this->phone2 = $phone2;
		$this->email = $email;
		$this->url = $url;
	}
}
class Contacts {
	private $file;
	private $contacts = array();
	public function show()
	{
		echo "\n";
		foreach($this->contacts as $contact) {
			$contact->showshort();
		}
		echo "
\n";
	}
	public function getNextCID()
	{
		$nextcid = 1;
		foreach($this->contacts as $contact) {
			if($nextcid < $contact->cid) $nextcid = $contact->cid;
		}
		return $nextcid;
	}
	public function getContact($cid)
	{
		return $this->contacts[$cid];
	}
	public function add($contact) {
		$key = $contact->cid;
		$this->contacts[$key] = $contact;
	}
	
	public function write()
	{
		$fp = fopen($this->file, "w");
    $block = TRUE;
    flock($fp, LOCK_EX, $block); // do an exclusive lock
		fwrite($fp, "\n");
		fwrite($fp, "\n");
		foreach($this->contacts as $contact) {
			$contact->write($fp);
		}
		fwrite($fp, "\n");
		fclose($fp);
	}
	
	private function read()
	{
		$dom = new DomDocument;
		$dom->preserveWhiteSpace = FALSE;
		$dom->load($this->file);
		$params = $dom->getElementsByTagName('contact');
		foreach ($params as $param) {
			$contact = new Contact($param->getAttribute('cid'),
														 $param->getAttribute('name'),
														 $param->getAttribute('address'),
														 $param->getAttribute('phone'),
														 $param->getAttribute('phone2'),
														 $param->getAttribute('email'),
														 $param->getAttribute('url'));
			$this->add($contact);
		}
	}
	public function Contacts($file)
	{
		$this->file = $file;
		if(file_exists($file)) $this->read();
	}
}
?>