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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
<?php
include_once($UTIL_DIR . "/convert.php");
include_once($UTIL_DIR . "/parser.php");
class Contact {
public $cid;
public $name;
public $co;
public $address;
public $city;
public $country;
public $phone;
public $phone2;
public $email;
public $email2;
public $url;
public $url2;
public $essential;
public $notes;
public function write($fp) {
fwrite($fp, " <contact cid=\"" . convert_xml($this->cid, ENT_QUOTES, "UTF-8") . "\"\n");
fwrite($fp, " name=\"" . convert_xml($this->name, ENT_QUOTES, "UTF-8") . "\"\n");
fwrite($fp, " co=\"" . convert_xml($this->co, ENT_QUOTES, "UTF-8") . "\"\n");
fwrite($fp, " address=\"" . convert_xml($this->address, ENT_QUOTES, "UTF-8") . "\"\n");
fwrite($fp, " city=\"" . convert_xml($this->city, ENT_QUOTES, "UTF-8") . "\"\n");
fwrite($fp, " country=\"" . convert_xml($this->country, ENT_QUOTES, "UTF-8") . "\"\n");
fwrite($fp, " phone=\"" . convert_xml($this->phone, ENT_QUOTES, "UTF-8") . "\"\n");
fwrite($fp, " phone2=\"" . convert_xml($this->phone2, ENT_QUOTES, "UTF-8") . "\"\n");
fwrite($fp, " email=\"" . convert_xml($this->email, ENT_QUOTES, "UTF-8") . "\"\n");
fwrite($fp, " email2=\"" . convert_xml($this->email2, ENT_QUOTES, "UTF-8") . "\"\n");
fwrite($fp, " url=\"" . convert_xml($this->url, ENT_QUOTES, "UTF-8") . "\"\n");
fwrite($fp, " url2=\"" . convert_xml($this->url2, ENT_QUOTES, "UTF-8") . "\"\n");
fwrite($fp, " essential=\"" . convert_xml($this->essential, ENT_QUOTES, "UTF-8") . "\">");
fwrite($fp, convert_xml($this->notes, ENT_QUOTES, "UTF-8"));
fwrite($fp, "</contact>\n");
}
public function show()
{
echo "<p>\n";
// echo "\tcid: " . $this->cid . "<br/>\n";
echo "\tName: " . $this->name . "<br/>\n";
if($this->co) echo "\tc/o: " . $this->co . "<br/>\n";
if($this->address) echo "\tAddress: " . $this->address . "<br/>\n";
if($this->city) echo "\tCity: " . $this->city . "<br/>\n";
if($this->country) echo "\tCountry: " . $this->country . "<br/>\n";
if($this->phone) echo "\tPhone: " . $this->phone . "<br/>\n";
if($this->phone2) echo "\tPhone2: " . $this->phone2 . "<br/>\n";
if($this->email) echo "\tEmail: <a href=\"mailto:" . $this->email . "\">" . $this->email . "</a><br/>\n";
if($this->email2) echo "\tEmail2: <a href=\"mailto:" . $this->email2 . "\">" . $this->email2 . "</a><br/>\n";
if($this->url) echo "\tURL: <a href=\"" . $this->url . "\">" . $this->url . "</a><br/>\n";
if($this->url2) echo "\tURL2: <a href=\"" . $this->url2 . "\">" . $this->url2 . "</a><br/>\n";
if($this->notes) echo "\tNotes: <br/>\n" . parse($this->notes) . "<br/>\n";
echo "\t<a href=\"?mode=addressbook&action=editcontact&cid=" . $this->cid . "\">Edit</a>\n";
echo "</p>\n";
}
public function showshort()
{
echo "<div class=\"contact\"><a href=\"?mode=addressbook&cid=" . $this->cid . "\">" . $this->name . "</a></div>";
}
function Contact($cid, $name, $co, $address, $city, $country, $phone, $phone2, $email, $email2, $url, $url2, $essential, $notes)
{
$this->cid = $cid;
$this->name = $name;
$this->co = $co;
$this->address = $address;
$this->city = $city;
$this->country = $country;
$this->phone = $phone;
$this->phone2 = $phone2;
$this->email = $email;
$this->email2 = $email2;
$this->url = $url;
$this->url2 = $url2;
$this->essential = $essential;
$this->notes = $notes;
}
}
class ContactGroup {
public $gid;
public $name;
private $contacts = array();
public function getContact($cid)
{
return $this->contacts[$cid];
}
public function write($fp) {
fwrite($fp, " <contactgroup gid=\"" . convert_xml($this->gid, ENT_QUOTES, "UTF-8") . "\"\n");
fwrite($fp, " name=\"" . convert_xml($this->name, ENT_QUOTES, "UTF-8") . "\">\n");
foreach($this->contacts as $contact) {
$contact->write($fp);
}
fwrite($fp, " </contactgroup>\n");
}
public function getMaxCID()
{
$maxcid = 1;
foreach($this->contacts as $contact) {
if($maxcid < $contact->cid) $maxcid = $contact->cid;
}
return $maxcid;
}
public function add($contact) {
$key = $contact->cid;
$this->contacts[$key] = $contact;
}
public function show()
{
echo " <div class=\"contactgroup\">\n";
echo " <div class=\"title\">" . $this->name . "</div>\n";
echo " <div class=\"contacts\">\n";
foreach($this->contacts as $contact) {
$contact->showshort();
}
echo " </div>\n";
echo " </div>\n";
}
public function showshort()
{
$ness = 0;
foreach($this->contacts as $contact) {
if($contact->essential) $ness += 1;
}
echo " <div class=\"contactgroup\">\n";
echo " <div class=\"title\"><a href=\"?mode=addressbook&gid=" . $this->gid . "\">" . $this->name . "</a> - ".sizeof($this->contacts)." contacts (". sprintf("%d", sizeof($this->contacts) - $ness)." hidden)</div>\n";
echo " <div class=\"contacts\">\n";
foreach($this->contacts as $contact) {
if($contact->essential) $contact->showshort();
}
echo " </div>\n";
echo " </div>\n";
}
function ContactGroup($gid,
$name)
{
$this->gid = $gid;
$this->name = $name;
}
}
class Contacts {
private $file;
private $contactgroups = array();
public function show()
{
echo "<div class=\"contactlist\">\n";
foreach($this->contactgroups as $contactgroup) {
$contactgroup->showshort();
}
echo "</div>\n";
}
public function getNextCID()
{
$nextcid = 1;
foreach($this->contactgroups as $contactgroup) {
$cid = $contactgroup->getMaxCID();
if($nextcid < $cid) $nextcid = $cid;
}
return $nextcid + 1;
}
public function getNextGID()
{
$nextgid = 1;
foreach($this->contactgroups as $contactgroup) {
if($nextgid < $contactgroup->gid) $nextgid = $contactgroup->gid;
}
return $nextgid + 1;
}
public function getContact($cid)
{
foreach($this->contactgroups as $contactgroup) {
if($contactgroup->getContact($cid)) return $contactgroup->getContact($cid);
}
return false;
}
public function getContactGroup($gid)
{
return $this->contactgroups[$gid];
}
public function add($contactgroup) {
$key = $contactgroup->gid;
$this->contactgroups[$key] = $contactgroup;
}
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->contactgroups as $contactgroup) {
$contactgroup->write($fp);
}
fwrite($fp, "</contacts>\n");
fclose($fp);
}
private function read()
{
$dom = new DomDocument;
$dom->preserveWhiteSpace = TRUE;
$dom->load($this->file);
$contactgroups = $dom->getElementsByTagName('contactgroup');
foreach ($contactgroups as $cg) {
$contactgroup = new ContactGroup($cg->getAttribute('gid'),
$cg->getAttribute('name'));
$contacts = $cg->getElementsByTagName('contact');
foreach ($contacts as $c) {
$contact = new Contact($c->getAttribute('cid'),
$c->getAttribute('name'),
$c->getAttribute('co'),
$c->getAttribute('address'),
$c->getAttribute('city'),
$c->getAttribute('country'),
$c->getAttribute('phone'),
$c->getAttribute('phone2'),
$c->getAttribute('email'),
$c->getAttribute('email2'),
$c->getAttribute('url'),
$c->getAttribute('url2'),
$c->getAttribute('essential'),
$c->textContent);
$contactgroup->add($contact);
}
$this->add($contactgroup);
}
}
public function Contacts($file)
{
$this->file = $file;
if(file_exists($file)) $this->read();
}
}
?>
|