root / nephthys_addressbook.php

View | Annotate | Download (8.2 KB)

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