1 | <?php |
---|
2 | /**************************************************************************\ |
---|
3 | * eGroupWare API - Accounts manager for SQL * |
---|
4 | * Written by Joseph Engo <jengo@phpgroupware.org> * |
---|
5 | * and Dan Kuykendall <seek3r@phpgroupware.org> * |
---|
6 | * and Bettina Gille [ceb@phpgroupware.org] * |
---|
7 | * View and manipulate account records using SQL * |
---|
8 | * Copyright (C) 2000 - 2002 Joseph Engo * |
---|
9 | * Copyright (C) 2003 Joseph Engo, Bettina Gille * |
---|
10 | * ------------------------------------------------------------------------ * |
---|
11 | * This library is part of the eGroupWare API * |
---|
12 | * http://www.egroupware.org * |
---|
13 | * ------------------------------------------------------------------------ * |
---|
14 | * This library is free software; you can redistribute it and/or modify it * |
---|
15 | * under the terms of the GNU Lesser General Public License as published by * |
---|
16 | * the Free Software Foundation; either version 2.1 of the License, * |
---|
17 | * or any later version. * |
---|
18 | * This library is distributed in the hope that it will be useful, but * |
---|
19 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
---|
21 | * See the GNU Lesser General Public License for more details. * |
---|
22 | * You should have received a copy of the GNU Lesser General Public License * |
---|
23 | * along with this library; if not, write to the Free Software Foundation, * |
---|
24 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
---|
25 | \**************************************************************************/ |
---|
26 | |
---|
27 | /*! |
---|
28 | @class_start accounts |
---|
29 | @abstract Class for handling user and group accounts |
---|
30 | */ |
---|
31 | class accounts_ |
---|
32 | { |
---|
33 | var $db; |
---|
34 | var $account_id; |
---|
35 | var $data; |
---|
36 | var $total; |
---|
37 | |
---|
38 | function accounts_() |
---|
39 | { |
---|
40 | //copyobj($GLOBALS['phpgw']->db,$this->db); |
---|
41 | $this->db = is_object($GLOBALS['phpgw']->db) ? $GLOBALS['phpgw']->db : $GLOBALS['phpgw_setup']->db; |
---|
42 | |
---|
43 | $this->table = 'phpgw_accounts'; |
---|
44 | $this->db->set_app('phpgwapi'); // to load the right table-definitions for insert, select, update, ... |
---|
45 | } |
---|
46 | |
---|
47 | function list_methods($_type='xmlrpc') |
---|
48 | { |
---|
49 | if (is_array($_type)) |
---|
50 | { |
---|
51 | $_type = $_type['type'] ? $_type['type'] : $_type[0]; |
---|
52 | } |
---|
53 | |
---|
54 | switch($_type) |
---|
55 | { |
---|
56 | case 'xmlrpc': |
---|
57 | $xml_functions = array( |
---|
58 | 'get_list' => array( |
---|
59 | 'function' => 'get_list', |
---|
60 | 'signature' => array(array(xmlrpcStruct)), |
---|
61 | 'docstring' => lang('Returns a full list of accounts on the system. Warning: This is return can be quite large') |
---|
62 | ), |
---|
63 | 'list_methods' => array( |
---|
64 | 'function' => 'list_methods', |
---|
65 | 'signature' => array(array(xmlrpcStruct,xmlrpcString)), |
---|
66 | 'docstring' => lang('Read this list of methods.') |
---|
67 | ) |
---|
68 | ); |
---|
69 | return $xml_functions; |
---|
70 | break; |
---|
71 | case 'soap': |
---|
72 | return $this->soap_functions; |
---|
73 | break; |
---|
74 | default: |
---|
75 | return array(); |
---|
76 | break; |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | /*! |
---|
81 | @function read_repository |
---|
82 | @abstract grabs the records from the data store |
---|
83 | */ |
---|
84 | function read_repository() |
---|
85 | { |
---|
86 | $this->db->select($this->table,'*',array('account_id'=>$this->account_id),__LINE__,__FILE__); |
---|
87 | $this->db->next_record(); |
---|
88 | |
---|
89 | $this->data['userid'] = $this->db->f('account_lid'); |
---|
90 | $this->data['account_id'] = $this->db->f('account_id'); |
---|
91 | $this->data['account_lid'] = $this->db->f('account_lid'); |
---|
92 | $this->data['firstname'] = $this->db->f('account_firstname'); |
---|
93 | $this->data['lastname'] = $this->db->f('account_lastname'); |
---|
94 | $this->data['fullname'] = $this->db->f('account_firstname') . ' ' . $this->db->f('account_lastname'); |
---|
95 | $this->data['lastlogin'] = $this->db->f('account_lastlogin'); |
---|
96 | $this->data['lastloginfrom'] = $this->db->f('account_lastloginfrom'); |
---|
97 | $this->data['lastpasswd_change'] = $this->db->f('account_lastpwd_change'); |
---|
98 | $this->data['status'] = $this->db->f('account_status'); |
---|
99 | $this->data['expires'] = $this->db->f('account_expires'); |
---|
100 | $this->data['person_id'] = $this->db->f('person_id'); |
---|
101 | $this->data['account_primary_group'] = $this->db->f('account_primary_group'); |
---|
102 | $this->data['email'] = $this->db->f('account_email'); |
---|
103 | |
---|
104 | return $this->data; |
---|
105 | } |
---|
106 | |
---|
107 | /*! |
---|
108 | @function save_repository |
---|
109 | @abstract saves the records to the data store |
---|
110 | */ |
---|
111 | function save_repository() |
---|
112 | { |
---|
113 | $this->db->update($this->table,array( |
---|
114 | 'account_firstname' => $this->data['firstname'], |
---|
115 | 'account_lastname' => $this->data['lastname'], |
---|
116 | 'account_status' => $this->data['status'], |
---|
117 | 'account_expires' => $this->data['expires'], |
---|
118 | 'account_lid' => $this->data['account_lid'], |
---|
119 | 'person_id' => $this->data['person_id'], |
---|
120 | 'account_primary_group' => $this->data['account_primary_group'], |
---|
121 | 'account_email' => $this->data['email'], |
---|
122 | ),array( |
---|
123 | 'account_id' => $this->account_id |
---|
124 | ),__LINE__,__FILE__); |
---|
125 | } |
---|
126 | |
---|
127 | function delete($accountid = '') |
---|
128 | { |
---|
129 | $account_id = get_account_id($accountid); |
---|
130 | |
---|
131 | /* Do this last since we are depending upon this record to get the account_lid above */ |
---|
132 | $this->db->lock(Array($this->table)); |
---|
133 | $this->db->delete($this->table,array('account_id'=>$account_id),__LINE__,__FILE__); |
---|
134 | $this->db->unlock(); |
---|
135 | } |
---|
136 | |
---|
137 | function get_list($_type='both',$start = '',$sort = '', $order = '', $query = '', $offset = '',$query_type='') |
---|
138 | { |
---|
139 | if (! $sort) |
---|
140 | { |
---|
141 | $sort = "DESC"; |
---|
142 | } |
---|
143 | |
---|
144 | if (!empty($order) && preg_match('/^[a-zA-Z_0-9, ]+$/',$order) && (empty($sort) || preg_match('/^(DESC|ASC|desc|asc)$/',$sort))) |
---|
145 | { |
---|
146 | $orderclause = "ORDER BY $order $sort"; |
---|
147 | } |
---|
148 | else |
---|
149 | { |
---|
150 | $orderclause = "ORDER BY account_lid ASC"; |
---|
151 | } |
---|
152 | |
---|
153 | switch($_type) |
---|
154 | { |
---|
155 | case 'accounts': |
---|
156 | $whereclause = "WHERE account_type = 'u'"; |
---|
157 | break; |
---|
158 | case 'groups': |
---|
159 | $whereclause = "WHERE account_type = 'g'"; |
---|
160 | break; |
---|
161 | default: |
---|
162 | $whereclause = ''; |
---|
163 | } |
---|
164 | |
---|
165 | if ($query) |
---|
166 | { |
---|
167 | if ($whereclause) |
---|
168 | { |
---|
169 | $whereclause .= ' AND ( '; |
---|
170 | } |
---|
171 | else |
---|
172 | { |
---|
173 | $whereclause = ' WHERE ( '; |
---|
174 | } |
---|
175 | switch($query_type) |
---|
176 | { |
---|
177 | case 'all': |
---|
178 | default: |
---|
179 | $query = '%'.$query; |
---|
180 | // fall-through |
---|
181 | case 'start': |
---|
182 | $query .= '%'; |
---|
183 | // fall-through |
---|
184 | case 'exact': |
---|
185 | $query = $this->db->quote($query); |
---|
186 | $whereclause .= " account_firstname LIKE $query OR account_lastname LIKE $query OR account_lid LIKE $query )"; |
---|
187 | break; |
---|
188 | case 'firstname': |
---|
189 | case 'lastname': |
---|
190 | case 'lid': |
---|
191 | case 'email': |
---|
192 | $query = $this->db->quote('%'.$query.'%'); |
---|
193 | $whereclause .= " account_$query_type LIKE $query )"; |
---|
194 | break; |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | $sql = "SELECT * FROM $this->table $whereclause $orderclause"; |
---|
199 | if ($offset) |
---|
200 | { |
---|
201 | $this->db->limit_query($sql,$start,__LINE__,__FILE__,$offset); |
---|
202 | } |
---|
203 | elseif (is_numeric($start)) |
---|
204 | { |
---|
205 | $this->db->limit_query($sql,$start,__LINE__,__FILE__); |
---|
206 | } |
---|
207 | else |
---|
208 | { |
---|
209 | $this->db->query($sql,__LINE__,__FILE__); |
---|
210 | } |
---|
211 | while ($this->db->next_record()) |
---|
212 | { |
---|
213 | $accounts[] = Array( |
---|
214 | 'account_id' => $this->db->f('account_id'), |
---|
215 | 'account_lid' => $this->db->f('account_lid'), |
---|
216 | 'account_type' => $this->db->f('account_type'), |
---|
217 | 'account_firstname' => $this->db->f('account_firstname'), |
---|
218 | 'account_lastname' => $this->db->f('account_lastname'), |
---|
219 | 'account_status' => $this->db->f('account_status'), |
---|
220 | 'account_expires' => $this->db->f('account_expires'), |
---|
221 | 'person_id' => $this->db->f('person_id'), |
---|
222 | 'account_primary_group' => $this->db->f('account_primary_group'), |
---|
223 | 'account_email' => $this->db->f('account_email'), |
---|
224 | ); |
---|
225 | } |
---|
226 | $this->db->query("SELECT count(*) FROM $this->table $whereclause"); |
---|
227 | $this->db->next_record(); |
---|
228 | $this->total = $this->db->f(0); |
---|
229 | |
---|
230 | return $accounts; |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | * converts a name / unique value from the accounts-table (account_lid,account_email) to an id |
---|
235 | */ |
---|
236 | function name2id($name,$which='account_lid') |
---|
237 | { |
---|
238 | $this->db->select($this->table,'account_id',array($which=>$name),__LINE__,__FILE__); |
---|
239 | if($this->db->next_record()) |
---|
240 | { |
---|
241 | return (int)$this->db->f('account_id'); |
---|
242 | } |
---|
243 | return False; |
---|
244 | } |
---|
245 | |
---|
246 | /** |
---|
247 | * converts an id to the corresponding value of the accounts-table (account_lid,account_email,account_firstname,...) |
---|
248 | */ |
---|
249 | function id2name($account_id,$which='account_lid') |
---|
250 | { |
---|
251 | $this->db->select($this->table,$this->db->name_quote($which),array('account_id'=>$account_id),__LINE__,__FILE__); |
---|
252 | if($this->db->next_record()) |
---|
253 | { |
---|
254 | return $this->db->f(0); |
---|
255 | } |
---|
256 | return False; |
---|
257 | } |
---|
258 | function get_context($dn) |
---|
259 | { |
---|
260 | return substr($dn, (strpos($dn, ',')+1)); |
---|
261 | } |
---|
262 | |
---|
263 | function get_organization($dn) |
---|
264 | { |
---|
265 | return ''; |
---|
266 | } |
---|
267 | function get_sector($dn) |
---|
268 | { |
---|
269 | return ''; |
---|
270 | } |
---|
271 | function get_type($account_id) |
---|
272 | { |
---|
273 | return $this->id2name($account_id,'account_type'); |
---|
274 | } |
---|
275 | |
---|
276 | function exists($account_lid) |
---|
277 | { |
---|
278 | static $by_id, $by_lid; |
---|
279 | |
---|
280 | $where = array(); |
---|
281 | if(is_numeric($account_lid)) |
---|
282 | { |
---|
283 | if(@isset($by_id[$account_lid]) && $by_id[$account_lid] != '') |
---|
284 | { |
---|
285 | return $by_id[$account_lid]; |
---|
286 | } |
---|
287 | $where['account_id'] = $account_lid; |
---|
288 | } |
---|
289 | else |
---|
290 | { |
---|
291 | if(@isset($by_lid[$account_lid]) && $by_lid[$account_lid] != '') |
---|
292 | { |
---|
293 | return $by_lid[$account_lid]; |
---|
294 | } |
---|
295 | $where['account_lid'] = $account_lid; |
---|
296 | } |
---|
297 | |
---|
298 | $this->db->select($this->table,'count(*)',$where,__LINE__,__FILE__); |
---|
299 | $this->db->next_record(); |
---|
300 | $ret_val = $this->db->f(0) > 0; |
---|
301 | if(is_numeric($account_lid)) |
---|
302 | { |
---|
303 | $by_id[$account_lid] = $ret_val; |
---|
304 | $by_lid[$this->id2name($account_lid)] = $ret_val; |
---|
305 | } |
---|
306 | else |
---|
307 | { |
---|
308 | $by_lid[$account_lid] = $ret_val; |
---|
309 | $by_id[$this->name2id($account_lid)] = $ret_val; |
---|
310 | } |
---|
311 | return $ret_val; |
---|
312 | } |
---|
313 | |
---|
314 | function create($account_info) |
---|
315 | { |
---|
316 | $account_data = array( |
---|
317 | 'account_lid' => $account_info['account_lid'], |
---|
318 | 'account_pwd' => $GLOBALS['phpgw']->common->encrypt_password($account_info['account_passwd'],True), |
---|
319 | 'account_firstname' => $account_info['account_firstname'], |
---|
320 | 'account_lastname' => $account_info['account_lastname'], |
---|
321 | 'account_status' => $account_info['account_status'], |
---|
322 | 'account_expires' => $account_info['account_expires'], |
---|
323 | 'account_type' => $account_info['account_type'], |
---|
324 | 'person_id' => $account_info['person_id'], |
---|
325 | 'account_primary_group' => $account_info['account_primary_group'], |
---|
326 | 'account_email' => $account_info['account_email'], |
---|
327 | ); |
---|
328 | if (isset($account_info['account_id']) && (int)$account_info['account_id'] && !$this->id2name($account_info['account_id'])) |
---|
329 | { |
---|
330 | // only use account_id, if it's not already used |
---|
331 | $account_data['account_id'] = $account_info['account_id']; |
---|
332 | } |
---|
333 | $this->db->insert($this->table,$account_data,False,__LINE__,__FILE__); |
---|
334 | |
---|
335 | return $this->db->get_last_insert_id($this->table,'account_id'); |
---|
336 | } |
---|
337 | |
---|
338 | function auto_add($accountname, $passwd, $default_prefs = False, $default_acls = False, $expiredate = 0, $account_status = 'A') |
---|
339 | { |
---|
340 | if ($expiredate == 0) |
---|
341 | { |
---|
342 | if(isset($GLOBALS['phpgw_info']['server']['auto_create_expire']) == True) |
---|
343 | { |
---|
344 | if($GLOBALS['phpgw_info']['server']['auto_create_expire'] == 'never') |
---|
345 | { |
---|
346 | $expires = -1; |
---|
347 | } |
---|
348 | else |
---|
349 | { |
---|
350 | $expiredate = time() + $GLOBALS['phpgw_info']['server']['auto_create_expire']; |
---|
351 | } |
---|
352 | } |
---|
353 | } |
---|
354 | else |
---|
355 | { |
---|
356 | /* expire in 30 days by default */ |
---|
357 | $expiredate = time() + ((60 * 60) * (30 * 24)); |
---|
358 | } |
---|
359 | |
---|
360 | if ($expires != -1) |
---|
361 | { |
---|
362 | $expires = mktime(2,0,0,date('n',$expiredate), (int)date('d',$expiredate), date('Y',$expiredate)); |
---|
363 | } |
---|
364 | |
---|
365 | $default_group_id = $this->name2id($GLOBALS['phpgw_info']['server']['default_group_lid']); |
---|
366 | if (!$default_group_id) |
---|
367 | { |
---|
368 | $default_group_id = (int) $this->name2id('Default'); |
---|
369 | } |
---|
370 | $primary_group = $GLOBALS['auto_create_acct']['primary_group'] && |
---|
371 | $this->get_type((int)$GLOBALS['auto_create_acct']['primary_group']) == 'g' ? |
---|
372 | (int) $GLOBALS['auto_create_acct']['primary_group'] : $default_group_id; |
---|
373 | |
---|
374 | $acct_info = array( |
---|
375 | 'account_id' => (int) $GLOBALS['auto_create_acct']['id'], |
---|
376 | 'account_lid' => $accountname, |
---|
377 | 'account_type' => 'u', |
---|
378 | 'account_passwd' => $passwd, |
---|
379 | 'account_firstname' => $GLOBALS['auto_create_acct']['firstname'] ? $GLOBALS['auto_create_acct']['firstname'] : 'New', |
---|
380 | 'account_lastname' => $GLOBALS['auto_create_acct']['lastname'] ? $GLOBALS['auto_create_acct']['lastname'] : 'User', |
---|
381 | 'account_status' => $account_status, |
---|
382 | 'account_expires' => $expires, |
---|
383 | 'account_primary_group' => $primary_group, |
---|
384 | ); |
---|
385 | |
---|
386 | /* attempt to set an email address */ |
---|
387 | if (isset($GLOBALS['auto_create_acct']['email']) == True && $GLOBALS['auto_create_acct']['email'] != '') |
---|
388 | { |
---|
389 | $acct_info['account_email'] = $GLOBALS['auto_create_acct']['email']; |
---|
390 | } |
---|
391 | elseif(isset($GLOBALS['phpgw_info']['server']['mail_suffix']) == True && $GLOBALS['phpgw_info']['server']['mail_suffix'] != '') |
---|
392 | { |
---|
393 | $acct_info['account_email'] = $accountname . '@' . $GLOBALS['phpgw_info']['server']['mail_suffix']; |
---|
394 | } |
---|
395 | |
---|
396 | $this->db->transaction_begin(); |
---|
397 | |
---|
398 | $this->create($acct_info); /* create the account */ |
---|
399 | |
---|
400 | $accountid = $this->name2id($accountname); /* grab the account id or an error code */ |
---|
401 | |
---|
402 | if ($accountid) /* begin account setup */ |
---|
403 | { |
---|
404 | /* If we have a primary_group, add it as "regular" eGW group (via ACL) too. */ |
---|
405 | if ($primary_group) |
---|
406 | { |
---|
407 | $this->db->query("insert into phpgw_acl (acl_appname, acl_location, acl_account, acl_rights) values('phpgw_group', " |
---|
408 | . $primary_group . ', ' . $accountid . ', 1)',__LINE__,__FILE__); |
---|
409 | } |
---|
410 | |
---|
411 | /* if we have an mail address set it in the uesrs' email preference */ |
---|
412 | if (isset($GLOBALS['auto_create_acct']['email']) && $GLOBALS['auto_create_acct']['email'] != '') |
---|
413 | { |
---|
414 | $GLOBALS['phpgw']->acl->acl($accountid); /* needed als preferences::save_repository calls acl */ |
---|
415 | $GLOBALS['phpgw']->preferences->preferences($accountid); |
---|
416 | $GLOBALS['phpgw']->preferences->read_repository(); |
---|
417 | $GLOBALS['phpgw']->preferences->add('email','address',$GLOBALS['auto_create_acct']['email']); |
---|
418 | $GLOBALS['phpgw']->preferences->save_repository(); |
---|
419 | } |
---|
420 | /* use the default mail domain to set the uesrs' email preference */ |
---|
421 | elseif(isset($GLOBALS['phpgw_info']['server']['mail_suffix']) && $GLOBALS['phpgw_info']['server']['mail_suffix'] != '') |
---|
422 | { |
---|
423 | $GLOBALS['phpgw']->acl->acl($accountid); /* needed als preferences::save_repository calls acl */ |
---|
424 | $GLOBALS['phpgw']->preferences->preferences($accountid); |
---|
425 | $GLOBALS['phpgw']->preferences->read_repository(); |
---|
426 | $GLOBALS['phpgw']->preferences->add('email','address', $accountname . '@' . $GLOBALS['phpgw_info']['server']['mail_suffix']); |
---|
427 | $GLOBALS['phpgw']->preferences->save_repository(); |
---|
428 | } |
---|
429 | |
---|
430 | /* commit the new account transaction */ |
---|
431 | $this->db->transaction_commit(); |
---|
432 | |
---|
433 | /* does anyone know what the heck this is required for? */ |
---|
434 | $GLOBALS['hook_values']['account_lid'] = $acct_info['account_lid']; |
---|
435 | $GLOBALS['hook_values']['account_id'] = $accountid; |
---|
436 | $GLOBALS['hook_values']['new_passwd'] = $acct_info['account_passwd']; |
---|
437 | $GLOBALS['hook_values']['account_status'] = $acct_info['account_status']; |
---|
438 | $GLOBALS['hook_values']['account_firstname'] = $acct_info['account_firstname']; |
---|
439 | $GLOBALS['hook_values']['account_lastname'] = $acct_info['account_lastname']; |
---|
440 | $GLOBALS['phpgw']->hooks->process($GLOBALS['hook_values']+array( |
---|
441 | 'location' => 'addaccount' |
---|
442 | ),False,True); /* called for every app now, not only enabled ones */ |
---|
443 | |
---|
444 | } /* end account setup */ |
---|
445 | else /* if no account id abort the account creation */ |
---|
446 | { |
---|
447 | $this->db->transaction_abort(); |
---|
448 | } |
---|
449 | |
---|
450 | /* |
---|
451 | * If we succeeded in creating the account (above), return the accountid, else, |
---|
452 | * return the error value from $this->name2id($accountname) |
---|
453 | */ |
---|
454 | return $accountid; |
---|
455 | |
---|
456 | } /* end auto_add() */ |
---|
457 | |
---|
458 | function get_account_name($accountid,&$lid,&$fname,&$lname) |
---|
459 | { |
---|
460 | $this->db->select($this->table,'account_lid,account_firstname,account_lastname',array('account_id'=>$accountid),__LINE__,__FILE__); |
---|
461 | if (!$this->db->next_record()) |
---|
462 | { |
---|
463 | return False; |
---|
464 | } |
---|
465 | $lid = $this->db->f('account_lid'); |
---|
466 | $fname = $this->db->f('account_firstname'); |
---|
467 | $lname = $this->db->f('account_lastname'); |
---|
468 | |
---|
469 | return True; |
---|
470 | } |
---|
471 | } |
---|
472 | /*! |
---|
473 | @class_end accounts |
---|
474 | */ |
---|