root / nephthys_addressbook.php

View | Annotate | Download (8.2 KB)

1
<?php
2
3
/***************************************************************************
4
 *
5
 * Nephthys - file sharing management
6
 * Copyright (c) by Andreas Unterkircher, unki@netshadow.at
7
 *
8
 *  This file is part of Nephthys.
9
 *
10
 *  Nephthys is free software: you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation, either version 3 of the License, or
13
 *  (at your option) any later version.
14
 *
15
 *  Nephthys is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with Nephthys. If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 ***************************************************************************/
24
25
class NEPHTHYS_ADDRESSBOOK {
26
27
   private $db;
28
   private $parent;
29
   private $tmpl;
30
   private $id;
31
32
   /**
33
    * NEPHTHS_ADDRESSBOOK constructor
34
    *
35
    * Initialize the NEPHTHS_ADDRESSBOOK class
36
    */
37
   public function __construct($id = NULL)
38
   {
39
      global $nephthys;
40
      $this->parent =& $nephthys;
41
      $this->db =& $nephthys->db;
42
      $this->tmpl =& $nephthys->tmpl;
43
44
      if(!empty($id))
45
         $this->id = $id;
46
47
      $this->tmpl->register_block("contact_list", array(&$this, "smarty_contact_list"));
48
49
      $query_str = "
50
         SELECT
51
            *
52
         FROM
53
            nephthys_addressbook ab
54
      ";
55
56
      if(!$this->parent->check_privileges('admin') &&
57
         !$this->parent->check_privileges('manager')) {
58
         $query_str.= "WHERE contact_owner LIKE '". $_SESSION['login_idx'] ."'";
59
      }
60
61
      /* get the current sort-order */
62
      $column = $this->parent->get_sort_column('addressbook');
63
      $order  = $this->parent->get_sort_order('addressbook');
64
65
      // if sort should happen on bucket-owners, sort by the real
66
      // user_name instead of the user_idx (which is stored in
67
      // bucket_owner).
68
      if($column == 'contact_owner') {
69
         $query_str.= "
70
            INNER JOIN
71
               nephthys_users u
72
            ON
73
               ab.contact_owner=u.user_idx
74
            ORDER BY
75
               u.user_name ". $order;
76
      }
77
      else {
78
         $query_str.= "
79
            ORDER BY
80
               ". $column ." ". $order;
81
      }
82
83
      $res_contacts = $nephthys->db->db_query($query_str);
84
85
      $cnt_contacts = 0;
86
87
      while($contact = $res_contacts->fetchrow()) {
88
         $this->avail_contacts[$cnt_contacts] = $contact->contact_idx;
89
         $this->contacts[$contact->contact_idx] = $contact;
90
         $cnt_contacts++;
91
      }
92
93
      $this->tmpl->assign('user_has_contacts', $cnt_contacts);
94
95
   } // __construct()
96
97
   /* interface output */
98
   public function show()
99
   {
100
      if(!$this->parent->is_logged_in()) {
101
         $this->parent->_error($this->parent->_("##MANAGE_USERS##") ." - ". $this->parent->_("##NOT_ALLOWED##"));
102
         return 0;
103
      }
104
       if(!isset($_GET['mode']))
105
         $_GET['mode'] = "show";
106
      if(!isset($_GET['idx']) ||
107
         (isset($_GET['idx']) && !is_numeric($_GET['idx'])))
108
         $_GET['idx'] = 0;
109
110
      switch($_GET['mode']) {
111
         default:
112
         case 'show':
113
            return $this->showList();
114
            break;
115
         case 'edit':
116
            return $this->showEdit($_GET['idx']);
117
            break;
118
      }
119
120
   } // show()
121
122
   public function store()
123
   {
124
      /* if not a privilged user, then set the owner to his id */
125
      if($this->parent->check_privileges('user')) {
126
         $_POST['contact_owner'] = $_SESSION['login_idx'];
127
      }
128
129
      isset($_POST['contact_new']) && $_POST['contact_new'] == 1 ? $new = 1 : $new = NULL;
130
131
      if(!isset($_POST['contact_email']) || empty($_POST['contact_email'])) {
132
         return $this->parent->_("##FAILURE_ENTER_EMAIL##");
133
      }
134
      if(!$this->parent->is_valid_email($_POST['contact_email'])) {
135
         return $this->parent->_("##FAILURE_ENTER_SENDER##");
136
      }
137
138
      if(isset($new)) {
139
140
         $sth = $this->db->db_prepare("
141
            INSERT INTO nephthys_addressbook (
142
               contact_idx, contact_name,
143
               contact_email, contact_owner
144
            ) VALUES (
145
               NULL, ?, ?, ?
146
            )
147
         ");
148
149
         $this->db->db_execute($sth, array(
150
            $_POST['contact_name'],
151
            $_POST['contact_email'],
152
            $_POST['contact_owner'],
153
         ));
154
155
         $this->id = $this->db->db_getid();
156
157
      }
158
      else {
159
160
            $sth = $this->db->db_prepare("
161
               UPDATE nephthys_addressbook
162
               SET
163
                  contact_name=?,
164
                  contact_email=?,
165
                  contact_owner=?
166
               WHERE
167
                  contact_idx=?
168
            ");
169
170
            $this->db->db_execute($sth, array(
171
               $_POST['contact_name'],
172
               $_POST['contact_email'],
173
               $_POST['contact_owner'],
174
               $_POST['contact_idx'],
175
            ));
176
      }
177
178
      return "ok";
179
180
   } // store()
181
182
   public function showList()
183
   {
184
      return $this->tmpl->fetch("addressbook_list.tpl");
185
186
   } // showList()
187
188
   /**
189
    * template function which will be called from the addressbook listing template
190
    */
191
   public function smarty_contact_list($params, $content, &$smarty, &$repeat)
192
   {
193
      $index = $this->tmpl->get_template_vars('smarty.IB.contact_list.index');
194
      if(!$index) {
195
         $index = 0;
196
      }
197
198
      if($index < count($this->avail_contacts)) {
199
200
         $contact_idx = $this->avail_contacts[$index];
201
         $contact =  $this->contacts[$contact_idx];
202
203
         $user_priv = $this->parent->get_user_priv($_SESSION['login_idx']);
204
         $contact_owner = $this->parent->get_user_name($contact->contact_owner);
205
206
         $this->tmpl->assign('contact_idx', $contact_idx);
207
208
         if(isset($contact->contact_name) && !empty($contact->contact_name)) {
209
            $this->tmpl->assign(
210
               'contact_name',
211
               $contact->contact_name ."&nbsp;&lt;". $contact->contact_email ."&gt;"
212
            );
213
         }
214
         else {
215
            $this->tmpl->assign('contact_name', $contact->contact_email);
216
         }
217
         $this->tmpl->assign('contact_owner', $contact_owner);
218
         $this->tmpl->assign('contact_owner_idx', $contact->contact_owner);
219
220
         $index++;
221
         $this->tmpl->assign('smarty.IB.contact_list.index', $index);
222
         $repeat = true;
223
      }
224
      else {
225
         $repeat =  false;
226
      }
227
228
      return $content;
229
230
   } // smarty_contact_list()
231
232
   public function delete()
233
   {
234
      if(isset($_POST['idx']) && is_numeric($_POST['idx'])) {
235
236
         /* ensure unprivileged users can only delete their own contacts */
237
         if($this->parent->check_privileges('user') && !$this->parent->is_contact_owner($_POST['idx'])) {
238
            return "You are only allowed to delete contacts you own!";
239
         }
240
241
         $this->db->db_query("
242
            DELETE FROM nephthys_addressbook
243
            WHERE contact_idx LIKE '". $_POST['idx'] ."'
244
         ");
245
      }
246
247
      print "ok";
248
249
   } // delete()
250
251
   /**
252
    * display interface to create or edit addressbook entires
253
    *
254
    * @param int $idx
255
    */
256
   private function showEdit($idx)
257
   {
258
      /* If authentication is enabled, check permissions */
259
      if(!$this->parent->is_logged_in()) {
260
         $this->parent->_error($this->parent->_("MANAGE_AB") ." - ". $this->parent->_("NOT_ALLOWED"));
261
         return 0;
262
      }
263
264
      if($idx != 0) {
265
         $contact = $this->db->db_fetchSingleRow("
266
            SELECT *
267
            FROM nephthys_addressbook
268
            WHERE
269
               contact_idx LIKE '". $idx ."'
270
         ");
271
272
         $this->tmpl->assign('contact_idx', $idx);
273
         $this->tmpl->assign('contact_name', $this->parent->unescape($contact->contact_name));
274
         $this->tmpl->assign('contact_email', $this->parent->unescape($contact->contact_email));
275
         $this->tmpl->assign('contact_owner', $this->parent->unescape($contact->contact_owner));
276
277
      }
278
279
      return $this->tmpl->fetch("addressbook_edit.tpl");
280
281
   } // showEdit()
282
283
} // class NEPHTHYS_ADDRESSBOOK
284
285
// vim: set filetype=php expandtab softtabstop=3 tabstop=3 shiftwidth=3 autoindent smartindent:
286
?>
287