-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnephthys_addressbook.php
286 lines (232 loc) · 8.19 KB
/
nephthys_addressbook.php
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/***************************************************************************
*
* Nephthys - file sharing management
* Copyright (c) by Andreas Unterkircher, [email protected]
*
* This file is part of Nephthys.
*
* Nephthys is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Nephthys is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Nephthys. If not, see <http://www.gnu.org/licenses/>.
*
***************************************************************************/
class NEPHTHYS_ADDRESSBOOK {
private $db;
private $parent;
private $tmpl;
private $id;
/**
* NEPHTHS_ADDRESSBOOK constructor
*
* Initialize the NEPHTHS_ADDRESSBOOK class
*/
public function __construct($id = NULL)
{
global $nephthys;
$this->parent =& $nephthys;
$this->db =& $nephthys->db;
$this->tmpl =& $nephthys->tmpl;
if(!empty($id))
$this->id = $id;
$this->tmpl->register_block("contact_list", array(&$this, "smarty_contact_list"));
$query_str = "
SELECT
*
FROM
nephthys_addressbook ab
";
if(!$this->parent->check_privileges('admin') &&
!$this->parent->check_privileges('manager')) {
$query_str.= "WHERE contact_owner LIKE '". $_SESSION['login_idx'] ."'";
}
/* get the current sort-order */
$column = $this->parent->get_sort_column('addressbook');
$order = $this->parent->get_sort_order('addressbook');
// if sort should happen on bucket-owners, sort by the real
// user_name instead of the user_idx (which is stored in
// bucket_owner).
if($column == 'contact_owner') {
$query_str.= "
INNER JOIN
nephthys_users u
ON
ab.contact_owner=u.user_idx
ORDER BY
u.user_name ". $order;
}
else {
$query_str.= "
ORDER BY
". $column ." ". $order;
}
$res_contacts = $nephthys->db->db_query($query_str);
$cnt_contacts = 0;
while($contact = $res_contacts->fetchrow()) {
$this->avail_contacts[$cnt_contacts] = $contact->contact_idx;
$this->contacts[$contact->contact_idx] = $contact;
$cnt_contacts++;
}
$this->tmpl->assign('user_has_contacts', $cnt_contacts);
} // __construct()
/* interface output */
public function show()
{
if(!$this->parent->is_logged_in()) {
$this->parent->_error($this->parent->_("##MANAGE_USERS##") ." - ". $this->parent->_("##NOT_ALLOWED##"));
return 0;
}
if(!isset($_GET['mode']))
$_GET['mode'] = "show";
if(!isset($_GET['idx']) ||
(isset($_GET['idx']) && !is_numeric($_GET['idx'])))
$_GET['idx'] = 0;
switch($_GET['mode']) {
default:
case 'show':
return $this->showList();
break;
case 'edit':
return $this->showEdit($_GET['idx']);
break;
}
} // show()
public function store()
{
/* if not a privilged user, then set the owner to his id */
if($this->parent->check_privileges('user')) {
$_POST['contact_owner'] = $_SESSION['login_idx'];
}
isset($_POST['contact_new']) && $_POST['contact_new'] == 1 ? $new = 1 : $new = NULL;
if(!isset($_POST['contact_email']) || empty($_POST['contact_email'])) {
return $this->parent->_("##FAILURE_ENTER_EMAIL##");
}
if(!$this->parent->is_valid_email($_POST['contact_email'])) {
return $this->parent->_("##FAILURE_ENTER_SENDER##");
}
if(isset($new)) {
$sth = $this->db->db_prepare("
INSERT INTO nephthys_addressbook (
contact_idx, contact_name,
contact_email, contact_owner
) VALUES (
NULL, ?, ?, ?
)
");
$this->db->db_execute($sth, array(
$_POST['contact_name'],
$_POST['contact_email'],
$_POST['contact_owner'],
));
$this->id = $this->db->db_getid();
}
else {
$sth = $this->db->db_prepare("
UPDATE nephthys_addressbook
SET
contact_name=?,
contact_email=?,
contact_owner=?
WHERE
contact_idx=?
");
$this->db->db_execute($sth, array(
$_POST['contact_name'],
$_POST['contact_email'],
$_POST['contact_owner'],
$_POST['contact_idx'],
));
}
return "ok";
} // store()
public function showList()
{
return $this->tmpl->fetch("addressbook_list.tpl");
} // showList()
/**
* template function which will be called from the addressbook listing template
*/
public function smarty_contact_list($params, $content, &$smarty, &$repeat)
{
$index = $this->tmpl->get_template_vars('smarty.IB.contact_list.index');
if(!$index) {
$index = 0;
}
if($index < count($this->avail_contacts)) {
$contact_idx = $this->avail_contacts[$index];
$contact = $this->contacts[$contact_idx];
$user_priv = $this->parent->get_user_priv($_SESSION['login_idx']);
$contact_owner = $this->parent->get_user_name($contact->contact_owner);
$this->tmpl->assign('contact_idx', $contact_idx);
if(isset($contact->contact_name) && !empty($contact->contact_name)) {
$this->tmpl->assign(
'contact_name',
$contact->contact_name ." <". $contact->contact_email .">"
);
}
else {
$this->tmpl->assign('contact_name', $contact->contact_email);
}
$this->tmpl->assign('contact_owner', $contact_owner);
$this->tmpl->assign('contact_owner_idx', $contact->contact_owner);
$index++;
$this->tmpl->assign('smarty.IB.contact_list.index', $index);
$repeat = true;
}
else {
$repeat = false;
}
return $content;
} // smarty_contact_list()
public function delete()
{
if(isset($_POST['idx']) && is_numeric($_POST['idx'])) {
/* ensure unprivileged users can only delete their own contacts */
if($this->parent->check_privileges('user') && !$this->parent->is_contact_owner($_POST['idx'])) {
return "You are only allowed to delete contacts you own!";
}
$this->db->db_query("
DELETE FROM nephthys_addressbook
WHERE contact_idx LIKE '". $_POST['idx'] ."'
");
}
print "ok";
} // delete()
/**
* display interface to create or edit addressbook entires
*
* @param int $idx
*/
private function showEdit($idx)
{
/* If authentication is enabled, check permissions */
if(!$this->parent->is_logged_in()) {
$this->parent->_error($this->parent->_("MANAGE_AB") ." - ". $this->parent->_("NOT_ALLOWED"));
return 0;
}
if($idx != 0) {
$contact = $this->db->db_fetchSingleRow("
SELECT *
FROM nephthys_addressbook
WHERE
contact_idx LIKE '". $idx ."'
");
$this->tmpl->assign('contact_idx', $idx);
$this->tmpl->assign('contact_name', $this->parent->unescape($contact->contact_name));
$this->tmpl->assign('contact_email', $this->parent->unescape($contact->contact_email));
$this->tmpl->assign('contact_owner', $this->parent->unescape($contact->contact_owner));
}
return $this->tmpl->fetch("addressbook_edit.tpl");
} // showEdit()
} // class NEPHTHYS_ADDRESSBOOK
// vim: set filetype=php expandtab softtabstop=3 tabstop=3 shiftwidth=3 autoindent smartindent:
?>