summaryrefslogtreecommitdiff
path: root/forum/utils/contacts.php
blob: cec6bd71ea77a3e0326d8c696671e8bdcd346901 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php

include_once($UTIL_DIR . "/convert.php");

class Contact {
	public $cid;
	public $name;
	public $address;
	public $phone;
	public $phone2;
	public $email;
	public $url;

	public function write($fp) {
		fwrite($fp, "  <contact cid=\"" . htmlspecialchars($this->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, "  </contact>\n");
	}

	public function show()
	{
		echo "<p>\n";
		echo "\tcid: " . $this->cid . "<br/>";
		echo "\tname: " . $this->name . "<br/>";
		echo "\taddress: " . $this->address . "<br/>";
		echo "\tphone: " . $this->phone . "<br/>";
		if($this->phone2) echo "\tphone2: " . $this->phone2 . "<br/>";
		echo "\temail: <a href=\"mailto:" . $this->email . "\">" . $this->email . "</a><br/>";
		echo "\turl: <a href=\"" . $this->url . "\">" . $this->url . "</a><br/>";
		echo "</p>\n";
	}

	public function showshort()
	{
		echo "<div class\"contact\"><a href=\"?mode=addressbook&amp;cid=" . $this->cid . "\">" . $this->name . "</a></div>";
	}

	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 "<div class=\"contactlist\">\n";
		foreach($this->contacts as $contact) {
			$contact->showshort();
		}
		echo "</div>\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, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

		fwrite($fp, "<contacts>\n");
		foreach($this->contacts as $contact) {
			$contact->write($fp);
		}
		fwrite($fp, "</contacts>\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();
	}

}
?>