From 9bca27c9342bd4f34bed77dd3eb8c51dd686cdf1 Mon Sep 17 00:00:00 2001 From: deva Date: Thu, 23 Oct 2008 08:43:13 +0000 Subject: More work on addressbook, now almost finished. (Added missing file) --- forum/utils/contacts.php | 139 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 forum/utils/contacts.php (limited to 'forum') diff --git a/forum/utils/contacts.php b/forum/utils/contacts.php new file mode 100644 index 0000000..cec6bd7 --- /dev/null +++ b/forum/utils/contacts.php @@ -0,0 +1,139 @@ +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 "
cid . "\">" . $this->name . "
"; + } + + 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(); + } + +} +?> \ No newline at end of file -- cgit v1.2.3