source: trunk/phpgwapi/inc/class.accounts_sql.inc.php @ 2

Revision 2, 16.0 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
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
259                function get_type($account_id)
260                {
261                        return $this->id2name($account_id,'account_type');
262                }
263
264                function exists($account_lid)
265                {
266                        static $by_id, $by_lid;
267
268                        $where = array();
269                        if(is_numeric($account_lid))
270                        {
271                                if(@isset($by_id[$account_lid]) && $by_id[$account_lid] != '')
272                                {
273                                        return $by_id[$account_lid];
274                                }
275                                $where['account_id'] = $account_lid;
276                        }
277                        else
278                        {
279                                if(@isset($by_lid[$account_lid]) && $by_lid[$account_lid] != '')
280                                {
281                                        return $by_lid[$account_lid];
282                                }
283                                $where['account_lid'] = $account_lid;
284                        }
285
286                        $this->db->select($this->table,'count(*)',$where,__LINE__,__FILE__);
287                        $this->db->next_record();
288                        $ret_val = $this->db->f(0) > 0;
289                        if(is_numeric($account_lid))
290                        {
291                                $by_id[$account_lid] = $ret_val;
292                                $by_lid[$this->id2name($account_lid)] = $ret_val;
293                        }
294                        else
295                        {
296                                $by_lid[$account_lid] = $ret_val;
297                                $by_id[$this->name2id($account_lid)] = $ret_val;
298                        }
299                        return $ret_val;
300                }
301
302                function create($account_info)
303                {
304                        $account_data = array(
305                                'account_lid'                   => $account_info['account_lid'],
306                                'account_pwd'                   => $GLOBALS['phpgw']->common->encrypt_password($account_info['account_passwd'],True),
307                                'account_firstname'             => $account_info['account_firstname'],
308                                'account_lastname'              => $account_info['account_lastname'],
309                                'account_status'                => $account_info['account_status'],
310                                'account_expires'               => $account_info['account_expires'],
311                                'account_type'                  => $account_info['account_type'],
312                                'person_id'                             => $account_info['person_id'],
313                                'account_primary_group' => $account_info['account_primary_group'],
314                                'account_email'                 => $account_info['account_email'],
315                        );
316                        if (isset($account_info['account_id']) && (int)$account_info['account_id'] && !$this->id2name($account_info['account_id']))
317                        {
318                                // only use account_id, if it's not already used
319                                $account_data['account_id'] = $account_info['account_id'];
320                        }
321                        $this->db->insert($this->table,$account_data,False,__LINE__,__FILE__);
322
323                        return $this->db->get_last_insert_id($this->table,'account_id');
324                }
325
326                function auto_add($accountname, $passwd, $default_prefs = False, $default_acls = False, $expiredate = 0, $account_status = 'A')
327                {
328                        if ($expiredate == 0)
329                        {
330                                if(isset($GLOBALS['phpgw_info']['server']['auto_create_expire']) == True)
331                                {
332                                        if($GLOBALS['phpgw_info']['server']['auto_create_expire'] == 'never')
333                                        {
334                                                $expires = -1;
335                                        }
336                                        else
337                                        {
338                                                $expiredate = time() + $GLOBALS['phpgw_info']['server']['auto_create_expire'];
339                                        }
340                                }
341                        }
342                        else
343                        {
344                                /* expire in 30 days by default */
345                                $expiredate = time() + ((60 * 60) * (30 * 24));
346                        }
347
348                        if ($expires != -1)
349                        {
350                                $expires = mktime(2,0,0,date('n',$expiredate), (int)date('d',$expiredate), date('Y',$expiredate));
351                        }
352
353                        $default_group_id  = $this->name2id($GLOBALS['phpgw_info']['server']['default_group_lid']);
354                        if (!$default_group_id)
355                        {
356                                $default_group_id = (int) $this->name2id('Default');
357                        }
358                        $primary_group = $GLOBALS['auto_create_acct']['primary_group'] &&
359                                $this->get_type((int)$GLOBALS['auto_create_acct']['primary_group']) == 'g' ?
360                                (int) $GLOBALS['auto_create_acct']['primary_group'] : $default_group_id;
361
362                        $acct_info = array(
363                                'account_id'        => (int) $GLOBALS['auto_create_acct']['id'],
364                                'account_lid'       => $accountname,
365                                'account_type'      => 'u',
366                                'account_passwd'    => $passwd,
367                                'account_firstname' => $GLOBALS['auto_create_acct']['firstname'] ? $GLOBALS['auto_create_acct']['firstname'] : 'New',
368                                'account_lastname'  => $GLOBALS['auto_create_acct']['lastname'] ? $GLOBALS['auto_create_acct']['lastname'] : 'User',
369                                'account_status'    => $account_status,
370                                'account_expires'   => $expires,
371                                'account_primary_group' => $primary_group,
372                        );
373
374                        /* attempt to set an email address */
375                        if (isset($GLOBALS['auto_create_acct']['email']) == True && $GLOBALS['auto_create_acct']['email'] != '')
376                        {
377                                $acct_info['account_email'] = $GLOBALS['auto_create_acct']['email'];
378                        }
379                        elseif(isset($GLOBALS['phpgw_info']['server']['mail_suffix']) == True && $GLOBALS['phpgw_info']['server']['mail_suffix'] != '')
380                        {
381                                $acct_info['account_email'] = $accountname . '@' . $GLOBALS['phpgw_info']['server']['mail_suffix'];
382                        }
383
384                        $this->db->transaction_begin();
385
386                        $this->create($acct_info); /* create the account */
387
388                        $accountid = $this->name2id($accountname); /* grab the account id or an error code */
389
390                        if ($accountid) /* begin account setup */
391                        {
392                                /* If we have a primary_group, add it as "regular" eGW group (via ACL) too. */
393                                if ($primary_group)
394                                {
395                                        $this->db->query("insert into phpgw_acl (acl_appname, acl_location, acl_account, acl_rights) values('phpgw_group', "
396                                                . $primary_group . ', ' . $accountid . ', 1)',__LINE__,__FILE__);
397                                }
398
399                                /* if we have an mail address set it in the uesrs' email preference */
400                                if (isset($GLOBALS['auto_create_acct']['email']) && $GLOBALS['auto_create_acct']['email'] != '')
401                                {
402                                        $GLOBALS['phpgw']->acl->acl($accountid);        /* needed als preferences::save_repository calls acl */
403                                        $GLOBALS['phpgw']->preferences->preferences($accountid);
404                                        $GLOBALS['phpgw']->preferences->read_repository();
405                                        $GLOBALS['phpgw']->preferences->add('email','address',$GLOBALS['auto_create_acct']['email']);
406                                        $GLOBALS['phpgw']->preferences->save_repository();
407                                }
408                                /* use the default mail domain to set the uesrs' email preference  */
409                                elseif(isset($GLOBALS['phpgw_info']['server']['mail_suffix']) && $GLOBALS['phpgw_info']['server']['mail_suffix'] != '')
410                                {
411                                        $GLOBALS['phpgw']->acl->acl($accountid);        /* needed als preferences::save_repository calls acl */
412                                        $GLOBALS['phpgw']->preferences->preferences($accountid);
413                                        $GLOBALS['phpgw']->preferences->read_repository();
414                                        $GLOBALS['phpgw']->preferences->add('email','address', $accountname . '@' . $GLOBALS['phpgw_info']['server']['mail_suffix']);
415                                        $GLOBALS['phpgw']->preferences->save_repository();
416                                }
417
418                                /* commit the new account transaction */
419                                $this->db->transaction_commit();
420
421                                /* does anyone know what the heck this is required for? */
422                                $GLOBALS['hook_values']['account_lid']  = $acct_info['account_lid'];
423                                $GLOBALS['hook_values']['account_id']   = $accountid;
424                                $GLOBALS['hook_values']['new_passwd']   = $acct_info['account_passwd'];
425                                $GLOBALS['hook_values']['account_status'] = $acct_info['account_status'];
426                                $GLOBALS['hook_values']['account_firstname'] = $acct_info['account_firstname'];
427                                $GLOBALS['hook_values']['account_lastname'] =  $acct_info['account_lastname'];
428                                $GLOBALS['phpgw']->hooks->process($GLOBALS['hook_values']+array(
429                                        'location' => 'addaccount'
430                                ),False,True);  /* called for every app now, not only enabled ones */
431
432                        } /* end account setup */
433                        else /* if no account id abort the account creation */
434                        {
435                                $this->db->transaction_abort();
436                        }
437
438                        /*
439                         * If we succeeded in creating the account (above), return the accountid, else,
440                         * return the error value from $this->name2id($accountname)
441                         */
442                        return $accountid;
443
444                } /* end auto_add() */
445
446                function get_account_name($accountid,&$lid,&$fname,&$lname)
447                {
448                        $this->db->select($this->table,'account_lid,account_firstname,account_lastname',array('account_id'=>$accountid),__LINE__,__FILE__);
449                        if (!$this->db->next_record())
450                        {
451                                return False;
452                        }
453                        $lid   = $this->db->f('account_lid');
454                        $fname = $this->db->f('account_firstname');
455                        $lname = $this->db->f('account_lastname');
456
457                        return True;
458                }
459        }
460        /*!
461         @class_end accounts
462        */
Note: See TracBrowser for help on using the repository browser.