source: companies/serpro/phpgwapi/inc/class.accounts_ldap.inc.php @ 903

Revision 903, 42.8 KB checked in by niltonneto, 15 years ago (diff)

Importacao inicial do Expresso do Serpro

Line 
1<?php
2        /**************************************************************************\
3        * eGroupWare API - Accounts manager for LDAP                               *
4        * Written by Joseph Engo <jengo@phpgroupware.org>                          *
5        *        and Lars Kneschke <lkneschke@phpgw.de>                            *
6        *        and Miles Lott <milos@groupwhere.org>                             *
7        *        and Bettina Gille <ceb@phpgroupware.org>                          *
8        * View and manipulate account records using LDAP                           *
9        * Copyright (C) 2000 - 2002 Joseph Engo, Lars Kneschke                     *
10        * Copyright (C) 2003 Lars Kneschke, Bettina Gille                          *
11        * ------------------------------------------------------------------------ *
12        * This library is part of the eGroupWare API                               *
13        * http://www.egroupware.org                                                *
14        * ------------------------------------------------------------------------ *
15        * This library is free software; you can redistribute it and/or modify it  *
16        * under the terms of the GNU Lesser General Public License as published by *
17        * the Free Software Foundation; either version 2.1 of the License,         *
18        * or any later version.                                                    *
19        * This library is distributed in the hope that it will be useful, but      *
20        * WITHOUT ANY WARRANTY; without even the implied warranty of               *
21        * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
22        * See the GNU Lesser General Public License for more details.              *
23        * You should have received a copy of the GNU Lesser General Public License *
24        * along with this library; if not, write to the Free Software Foundation,  *
25        * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            *
26        \**************************************************************************/
27
28        class accounts_
29        {
30                var $db;
31                var $ds;
32                var $account_id;
33                var $data;
34                var $user_context  = '';
35                var $group_context = '';
36                var $total;
37
38                function accounts_()
39                {
40                        $this->ds = $GLOBALS['phpgw']->common->ldapConnect();
41                        if(!@is_object($GLOBALS['phpgw']->translation))
42                        {
43                                $GLOBALS['phpgw']->translation = CreateObject('phpgwapi.translation');
44                        }
45                        $this->user_context  = $GLOBALS['phpgw_info']['server']['ldap_context'];
46                        $this->group_context = $GLOBALS['phpgw_info']['server']['ldap_group_context'];
47                }
48
49                function read_repository()
50                {
51                        $acct_type = $this->get_type($this->account_id);
52
53                        /* search the dn for the given uid */
54       
55//COLOCADO OS ATRIBUTOS UM A UM PORQUE O RHDN NAO DEVOLVE ALGUNS ATRIBUTOS A MENOS QUE SEJAM EXPRESSAMENTE INDICADOS
56                        if(($acct_type == 'g') && $this->group_context)
57                        {
58                                if(isset($GLOBALS['phpgw_info']['server']['atributoexpiracao']))
59                                        $sri = ldap_search($this->ds, $this->user_context, ("(&(gidnumber=" . (int)$this->account_id.")(phpgwaccounttype=g))"),array('gidnumber','cn','sn','givenname','uidnumber','uid','mail','homedirectory','loginshell','telephonenumber','mobile','pager','phpgwaccountlastlogin','phpgwaccountlastloginfrom','phpgwlastpasswdchange','phpgwaccountstatus','phpgwaccounttype','phpgwaccountexpires',$GLOBALS['phpgw_info']['server']['atributoexpiracao']));
60                                        else
61                                        $sri = ldap_search($this->ds, $this->user_context, ("(&(gidnumber=" . (int)$this->account_id.")(phpgwaccounttype=g))"),array('gidnumber','cn','sn','givenname','uidnumber','uid','mail','homedirectory','loginshell','telephonenumber','mobile','pager','phpgwaccountlastlogin','phpgwaccountlastloginfrom','phpgwlastpasswdchange','phpgwaccountstatus','phpgwaccounttype','phpgwaccountexpires'));
62                        }
63                        else
64                        {
65                                if(isset($GLOBALS['phpgw_info']['server']['atributoexpiracao']))
66                                        $sri = ldap_search($this->ds, $this->group_context, ("(&(uidnumber=" . (int)$this->account_id.")(phpgwaccounttype=u))"),array('gidnumber','cn','sn','givenname','uidnumber','uid','mail','homedirectory','loginshell','telephonenumber','mobile','pager','phpgwaccountlastlogin','phpgwaccountlastloginfrom','phpgwlastpasswdchange','phpgwaccountstatus','phpgwaccounttype','phpgwaccountexpires',$GLOBALS['phpgw_info']['server']['atributoexpiracao']));
67                                        else
68                                        $sri = ldap_search($this->ds, $this->group_context, ("(&(uidnumber=" . (int)$this->account_id.")(phpgwaccounttype=u))"),array('gidnumber','cn','sn','givenname','uidnumber','uid','mail','homedirectory','loginshell','telephonenumber','mobile','pager','phpgwaccountlastlogin','phpgwaccountlastloginfrom','phpgwlastpasswdchange','phpgwaccountstatus','phpgwaccounttype','phpgwaccountexpires'));
69
70                        }
71                        $allValues = ldap_get_entries($this->ds, $sri);
72
73                        /* Now dump it into the array; take first entry found */
74                        if($acct_type =='g')
75                        {
76                                $this->data['account_id']   = $allValues[0]['gidnumber'][0];
77                                $this->data['account_lid']  = $allValues[0]['cn'][0];
78                                $this->data['firstname']    = $GLOBALS['phpgw']->translation->convert($allValues[0]['cn'][0],'utf-8');
79                                $this->data['lastname']     = lang('Group');
80                        }
81                        else
82                        {
83                                $this->data['account_id']  = $allValues[0]['uidnumber'][0];
84                                $this->data['account_primary_group'] = $allValues[0]['gidnumber'][0];
85                                $this->data['account_lid'] = $allValues[0]['uid'][0];
86                                $this->data['firstname']   = $GLOBALS['phpgw']->translation->convert($allValues[0]['givenname'][0],'utf-8');
87                                $this->data['lastname']    = $GLOBALS['phpgw']->translation->convert($allValues[0]['sn'][0],'utf-8');
88                                if(isset($allValues[0]['mail'][0]))
89                                {
90                                        $this->data['email'] = $allValues[0]['mail'][0];
91                                }
92                        }
93                        $this->data['account_dn']  = $allValues[0]['dn'];
94                        $this->data['fullname']    = $GLOBALS['phpgw']->translation->convert($allValues[0]['cn'][0],'utf-8');
95
96                        if ($GLOBALS['phpgw_info']['server']['ldap_extra_attributes'])
97                        {
98                                $this->data['homedirectory']  = $allValues[0]['homedirectory'][0];
99                                $this->data['loginshell'] = $allValues[0]['loginshell'][0];
100                        }
101
102                        $this->data['telephonenumber']   = $allValues[0]['telephonenumber'][0];
103                        $this->data['mobile']   = $allValues[0]['mobile'][0];
104                        $this->data['pager']   = $allValues[0]['pager'][0];
105                        $this->data['lastlogin']         = $allValues[0]['phpgwaccountlastlogin'][0];
106                        $this->data['lastloginfrom']     = $allValues[0]['phpgwaccountlastloginfrom'][0];
107                        $this->data['lastpasswd_change'] = @$allValues[0]['phpgwlastpasswdchange'][0];
108                        $this->data['status']            = trim($allValues[0]['phpgwaccountstatus'][0]);
109                        $this->data['type']              = $allValues[0]['phpgwaccounttype'][0];
110                        //$this->data['expires']           = $allValues[0]['phpgwaccountexpires'][0];
111                        if(isset($GLOBALS['phpgw_info']['server']['atributoexpiracao']))
112                                        {
113                                        if(substr($allValues[0][$GLOBALS['phpgw_info']['server']['atributoexpiracao']][0],-1,1) == "Z")
114                                                {
115                                                $this->data['expires'] = strtotime($allValues[0][$GLOBALS['phpgw_info']['server']['atributoexpiracao']][0]);
116                                                }
117                                                else
118                                                {
119                                                $this->data['expires'] = $allValues[0][$GLOBALS['phpgw_info']['server']['atributoexpiracao']][0];
120                                                }
121                                        }
122                                else
123                                        {
124                                        $this->data['expires'] = $allValues[0]['phpgwaccountexpires'][0];
125                                        }
126                        //$this->data['expires'] = $allValues[0][$GLOBALS['phpgw_info']['server']['atributoexpiracao']][0];
127                        return $this->data;
128                }
129
130                function save_repository()
131                {
132                        #_debug_array($this->data);
133                        $acct_type = $this->get_type($this->account_id);
134
135                        /* search the dn for the given u/gidnumber */
136                        if(($acct_type == 'g') && $this->group_context)
137                        {
138                                $sri = ldap_search($this->ds, $this->group_context, ("(&(gidnumber=" . (int)$this->account_id.")(phpgwaccounttype=g))"));
139                        }
140                        else
141                        {
142                                $sri = ldap_search($this->ds, $this->user_context, ("(&(uidnumber=" . (int)$this->account_id.")(phpgwaccounttype=u))"));
143                        }
144                        $allValues = ldap_get_entries($this->ds, $sri);
145
146                        $this->data['account_type'] = $allValues[0]['phpgwaccounttype'][0];
147
148                        if($acct_type == 'u')
149                        {
150                                // data for posixaccount
151                                $newData['cn'] = $GLOBALS['phpgw']->translation->convert(sprintf("%s %s",
152                                        $this->data['firstname'],
153                                        $this->data['lastname']),$GLOBALS['phpgw']->translation->charset(),'utf-8'
154                                );
155                                $newData['uid'] = $GLOBALS['phpgw']->translation->convert(
156                                        $this->data['account_lid'],
157                                        $GLOBALS['phpgw']->translation->charset(),'utf-8'
158                                );
159                                if($this->data['lastname'])
160                                {
161                                        $newData['sn'] = $GLOBALS['phpgw']->translation->convert(
162                                                $this->data['lastname'],
163                                                $GLOBALS['phpgw']->translation->charset(),'utf-8'
164                                        );
165                                }
166
167                                if($this->data['firstname'])
168                                {
169                                        $newData['givenname'] = $GLOBALS['phpgw']->translation->convert(
170                                                $this->data['firstname'],
171                                                $GLOBALS['phpgw']->translation->charset(),'utf-8'
172                                        );
173                                }
174                                if ($GLOBALS['phpgw_info']['server']['ldap_extra_attributes'])
175                                {
176                                        $newData['homedirectory'] = $this->data['homedirectory'];
177                                        $newData['loginshell']    = $this->data['loginshell'];
178                                }
179                                else
180                                {
181                                        // the posixaccount schema requires this
182                                        $entry['homedirectory'] = '/home/'.$this->data['account_lid'];
183                                        $entry['loginshell']    = '/bin/false';
184                                }
185                                if($this->data['account_primary_group'])
186                                {
187                                        $newData['gidnumber'] = $this->data['account_primary_group'];
188                                }
189                                if($this->data['lastlogin'])
190                                {
191                                        $newData['phpgwaccountlastlogin'] = $this->data['lastlogin'];
192                                }
193                                if($this->data['lastloginfrom'])
194                                {
195                                        $newData['phpgwaccountlastloginfrom'] = $this->data['lastloginfrom'];
196                                }
197                                if($this->data['lastpasswd_change'])
198                                {
199                                        $newData['phpgwlastpasswdchange'] = $this->data['lastpasswd_change'];
200                                }
201                                if($this->data['status'])
202                                {
203                                        $newData['phpgwaccountstatus'] = $this->data['status'];
204                                }
205                                else
206                                {
207                                        $newData['phpgwaccountstatus'] = array();
208                                }
209                                if($this->data['expires'])
210                                {
211                                        //$newData['phpgwaccountexpires'] = $this->data['expires'];
212                                //$newData[$GLOBALS['phpgw_info']['server']['atributoexpiracao']] = $this->data['expires'];
213                                if(isset($GLOBALS['phpgw_info']['server']['atributoexpiracao']))
214                                                {
215                                                $newData[$GLOBALS['phpgw_info']['server']['atributoexpiracao']] = $this->data['expires'];
216                                                }
217                                        else
218                                                {
219                                                $newData['phpgwaccountexpires'] = $this->data['expires'];
220                                                }
221                                }
222                                if($this->data['email'])
223                                {
224                                        $newData['mail'] = $this->data['email'];
225                                }
226
227                                $newAccountID = $newData['uid'];
228                                $oldAccountID = $newData['uid'];
229                        }
230                        else
231                        {
232                                // data for posixgroup
233                                $newData['cn'] = $GLOBALS['phpgw']->translation->convert(
234                                        $this->data['account_lid'],
235                                        $GLOBALS['phpgw']->translation->charset(), 'utf-8'
236                                );
237                                $newData['gidnumber'] = $this->account_id;
238                                $newGroupID = $newData['cn'];
239                                $oldGroupID = $newData['cn'];
240                        }
241                        if($this->data['type'])
242                        {
243                                $newData['phpgwaccounttype'] = $this->data['type'];
244                        }
245
246                        /*
247                        Changing the uid:  Need to delete and add new, since
248                        PHP cannot change the dn for the entry.
249                        */
250                        if ($acct_type == 'g')
251                        {
252                                $test = $allValues[0]['cn'][0];
253                        }
254                        else
255                        {
256                                $test = $allValues[0]['uid'][0];
257                        }
258                        if($GLOBALS['phpgw']->translation->convert($test,'utf-8') != $this->data['account_lid'])
259                        {
260                                $oldData = $allValues[0];
261                                $oldDN   = $oldData['dn'];
262                                // remove all unneeded fields
263                                unset($oldData['dn']);
264                                unset($oldData['count']);
265                                foreach($oldData as $key => $value)
266                                {
267                                        if(is_numeric($key))
268                                        {
269                                                // remove the key, its no ldap key
270                                                unset($oldData[$key]);
271                                        }
272                                        else
273                                        {
274                                                // remove the count key
275                                                if($oldData[$key]['count'] == 1)
276                                                {
277                                                        $oldData[$key] = $value[0];
278                                                }
279                                                else
280                                                {
281                                                        unset($oldData[$key]['count']);
282                                                }
283                                        }
284                                }
285
286                                $oldAccountID = $oldData['uid'];
287                                $oldGroupID   = $oldData['cn'];
288
289                                // merge the old data with the new one
290                                $newData = array_merge($oldData, $newData);
291
292                                /* Groups */
293                                if($this->data['account_type'] == 'g' && $this->group_context )
294                                {
295                                        $newDN = 'cn='.$this->data['account_lid'].','.$this->group_context;
296                                        $members = $this->member($this->account_id);
297                                        $newData['memberuid'] = array();
298                                        for($i=0;$i<count($members);$i++)
299                                        {
300                                                $currname = $this->id2name($members[$i]['account_id']);
301                                                if(!in_array($currname,$entry['memberuid']))
302                                                {
303                                                        $newData['memberuid'][] = $currname;
304                                                }
305                                        }
306                                }
307                                /* Accounts */
308                                else
309                                {
310                                        $newDN = 'uid='.$this->data['account_lid'].','.$this->user_context;
311                                }
312                                // delete the old account
313                                ldap_delete($this->ds,$oldDN);
314
315                                // add the new account
316                                #_debug_array($newData);
317                                ldap_add($this->ds, $newDN, $newData);
318                        }
319                        /* Normal behavior for save_repository
320                           update Account */
321                        else
322                        {
323                                // add the list group members
324                                if($this->data['account_type'] == 'g' && $this->group_context )
325                                {
326                                        $members = $this->member($this->account_id);
327                                        #_debug_array($members);
328                                        $newData['memberuid'] = array();
329                                        for($i=0;$i<count($members);$i++)
330                                        {
331                                                $currname = $this->id2name($members[$i]['account_id']);
332                                                if(!in_array($currname,$newData['memberuid']))
333                                                {
334                                                        $newData['memberuid'][] = $currname;
335                                                }
336                                        }
337                                }
338                                // modify the DN
339                                ldap_modify($this->ds, $allValues[0]['dn'], $newData);
340                        }
341
342                        if ($this->data['account_type'] == 'u')
343                        {
344                                // lets check for groups, the user needs to be removed
345
346                                // first lets search for the groups, the user is currently member of
347                                // and from which he needs to be removed
348                                $filter    = "(&(objectclass=posixgroup)(memberuid=" . (int)$oldAccountID . "))";
349                                $justThese = array('memberuid','gidnumber');
350                                $sri = ldap_search($this->ds, $this->group_context, $filter, $justThese);
351                                if($sri)
352                                {
353                                        $allValues = ldap_get_entries($this->ds, $sri);
354                                        if($allValues['count'] > 0)
355                                        {
356                                                unset($allValues['count']);
357                                                foreach($allValues as $key)
358                                                {
359                                                        #_debug_array($key);
360                                                        #_debug_array($this->data['account_groups']);
361                                                        // delete the old accountid from any group
362                                                        if($newAccountID != $oldAccountID)
363                                                        {
364                                                                $dn = $key['dn'];
365                                                                $newData = array();
366                                                                $newData['memberuid'] = $key['memberuid'];
367                                                                unset($newData['memberuid']['count']);
368                                                                // remove the uid from memberuid
369                                                                $newData['memberuid'] = array_flip($newData['memberuid']);
370                                                                unset($newData['memberuid'][$oldAccountID]);
371                                                                # $newData['memberuid'] = array_values(sort(array_flip($newData['memberuid'])));
372                                                                $newData['memberuid'] = array_values(array_flip($newData['memberuid']));
373                                                                ldap_mod_replace($this->ds, $dn, $newData);
374                                                                #print ldap_error($this->ds);
375                                                        }
376                                                        else
377                                                        {
378                                                                if(!in_array($key['gidnumber'][0],$this->data['account_groups']))
379                                                                {
380                                                                        $dn = $key['dn'];
381                                                                        $newData = array();
382                                                                        $newData['memberuid'] = $key['memberuid'];
383                                                                        unset($newData['memberuid']['count']);
384                                                                        // remove the uid from memberuid
385                                                                        $newData['memberuid'] = array_flip($newData['memberuid']);
386                                                                        unset($newData['memberuid'][$oldAccountID]);
387                                                                        $newData['memberuid'] = array_values(sort(array_flip($newData['memberuid'])));
388                                                                        ldap_mod_replace($this->ds, $dn, $newData);
389                                                                        #print ldap_error($this->ds);
390                                                                }
391                                                        }
392                                                }
393                                        }
394                                }
395
396                                // lets check group the user needs to be added
397                                foreach($this->data['account_groups'] as $key => $value)
398                                {
399                                        // search for the group
400                                        $filter    = 'gidnumber=' . (int)$value;
401                                        $justThese = array('memberuid');
402                                        $sri = ldap_search($this->ds, $this->group_context, $filter, $justThese);
403                                        if($sri)
404                                        {
405                                                $allValues = ldap_get_entries($this->ds, $sri);
406                                                // if the user is not member of this group, add him
407                                                if(is_array($allValues[0]['memberuid']))
408                                                {
409                                                        // this group has already some members
410                                                        if(!in_array($newData['uid'],$allValues[0]['memberuid']))
411                                                        {
412                                                                $dn = $allValues[0]['dn'];
413                                                                $newData = array();
414                                                                $newData['memberuid'] = $allValues[0]['memberuid'];
415                                                                unset($newData['memberuid']['count']);
416                                                                $newData['memberuid'][] = $newAccountID;
417                                                                $newData['memberuid'] = array_values(array_unique($newData['memberuid']));
418                                                                ldap_mod_replace($this->ds, $dn, $newData);
419                                                        }
420                                                }
421                                                else
422                                                {
423                                                        // this group has no members
424                                                        $dn = $allValues[0]['dn'];
425                                                        $newData = array();
426                                                        $newData['memberuid'][] = $newAccountID;
427                                                        ldap_mod_replace($this->ds, $dn, $newData);
428                                                }
429                                        }
430                                }
431                        }
432                }
433
434                function delete($accountid = '')
435                {
436                        $account_id = get_account_id($accountid);
437                        $account_lid = $this->id2name((int)$account_id);
438
439                        $filter = "(&(gidnumber=" . (int)$account_id.")(phpgwaccounttype=g))";
440                        $sri = ldap_search($this->ds, $this->group_context, $filter);
441                        if($sri)
442                        {
443                                $allValues = ldap_get_entries($this->ds, $sri);
444                        }
445
446                        if(!$allValues[0]['dn'])
447                        {
448                                $sri = ldap_search($this->ds, $this->user_context, "(&(uid=" . (string)$account_lid.")(phpgwaccounttype=u))");
449                                if($sri)
450                                {
451                                        $allValues = ldap_get_entries($this->ds, $sri);
452                                        $accountID = $allValues['0']['uid'][0];
453                                        $wasAccount = True;
454                                }
455                        }
456
457                        if ($allValues[0]['dn'])
458                        {
459                                $del = ldap_delete($this->ds, $allValues[0]['dn']);
460                        }
461
462                        if($wasAccount)
463                        {
464                                // remove the user from any group he is member of
465                                $filter    = "(&(objectclass=posixgroup)(memberuid=" . (int)$accountID . "))";
466                                $justThese = array('memberuid','gidnumber');
467                                $sri = ldap_search($this->ds, $this->group_context, $filter, $justThese);
468                                if($sri)
469                                {
470                                        $allValues = ldap_get_entries($this->ds, $sri);
471                                        if($allValues['count'] > 0)
472                                        {
473                                                unset($allValues['count']);
474                                                foreach($allValues as $key)
475                                                {
476                                                        $dn = $key['dn'];
477                                                        $newData = array();
478                                                        $newData['memberuid'] = $key['memberuid'];
479                                                        unset($newData['memberuid']['count']);
480                                                        // remove the uid from memberuid
481                                                        $newData['memberuid'] = array_flip($newData['memberuid']);
482                                                        unset($newData['memberuid'][$accountID]);
483                                                        $newData['memberuid'] = array_unique(array_flip($newData['memberuid']));
484                                                        ldap_mod_replace($this->ds, $dn, $newData);
485                                                }
486                                        }
487                                }
488                        }
489                }
490
491                function get_list($_type='both', $start = '',$sort = '', $order = '', $query = '', $offset = '',$query_type='',$new_filter='')
492                {
493                        //print "\$_type=$_type, \$start=$start , \$sort=$sort, \$order=$order, \$query=$query, \$offset=$offset, \$query_type=$query_type<br>";
494                        $query = strtolower($query);
495
496                        if($_type == 'accounts' || $_type == 'both')
497                        {
498                                $filter = "(&(uidnumber=*)(phpgwaccounttype=u)";
499                                if ($query_type == 'filter' || (!empty($query) && $query != '*'))
500                                {
501                                        switch($query_type)
502                                        {
503                                                case 'all':
504                                                default:
505                                                        $query = '*'.$query;
506                                                        // fall-through
507                                                case 'start':
508                                                        $query .= '*';
509                                                        // fall-through
510                                                case 'exact':
511                                                        $filter .= "(|(uid=$query)(sn=$query)(cn=$query)(givenname=$query)(mail=$query))";
512                                                        break;
513                                                // use the parameter $query as a customized filter to search for accounts
514                                                case 'filter':
515                                                        $filter = $new_filter;
516                                                        break;
517                                                case 'firstname':
518                                                case 'lastname':
519                                                case 'lid':
520                                                case 'email':
521                                                        $to_ldap = array(
522                                                                'firstname' => 'givenname',
523                                                                'lastname'  => 'sn',
524                                                                'lid'       => 'uid',
525                                                                'email'     => 'mail',
526                                                        );
527                                                        $filter .= '('.$to_ldap[$query_type].'=*'.$query.'*)';
528                                                        break;
529                                        }
530                                }
531                                if ($query_type != 'filter'){
532                                        $filter .= ')';
533                                }
534
535                                $sri = ldap_search($this->ds, $this->user_context, $filter);
536                                $allValues = ldap_get_entries($this->ds, $sri);
537                                while (list($null,$allVals) = @each($allValues))
538                                {
539                                        settype($allVals,'array');
540                                        $test = @$allVals['uid'][0];
541                                        if (!$GLOBALS['phpgw_info']['server']['global_denied_users'][$test] && $allVals['uid'][0])
542                                        {
543                                                $accounts[] = Array(
544                                                        'account_id'        => $allVals['uidnumber'][0],
545                                                        'account_lid'       => $allVals['uid'][0],
546                                                        'account_type'      => $allVals['phpgwaccounttype'][0],
547                                                        'account_common_name' => $GLOBALS['phpgw']->translation->convert($allVals['cn'][0],'utf-8'),
548                                                        'account_firstname' => $GLOBALS['phpgw']->translation->convert($allVals['givenname'][0],'utf-8'),
549                                                        'account_lastname'  => $GLOBALS['phpgw']->translation->convert($allVals['sn'][0],'utf-8'),
550                                                        'account_status'    => $allVals['phpgwaccountstatus'][0],
551                                                        'account_email'     => $allVals['mail'][0],
552                                                );
553                                        }
554                                }
555                        }
556                        if ($_type == 'groups' || $_type == 'both')
557                        {
558                                if(empty($query) || $query == '*')
559                                {
560                                        $filter = '(&(gidnumber=*)(phpgwaccounttype=g))';
561                                }
562                                else
563                                {
564                                        $filter = "(&(gidnumber=*)(phpgwaccounttype=g)(|(uid=*$query*)(sn=*$query*)(cn=*$query*)(givenname=*$query*)))";
565                                }
566                                $sri = ldap_search($this->ds, $this->group_context, $filter);
567                                $allValues = ldap_get_entries($this->ds, $sri);
568                                while (list($null,$allVals) = @each($allValues))
569                                {
570                                        settype($allVals,'array');
571                                        $test = $allVals['cn'][0];
572                                        if (!$GLOBALS['phpgw_info']['server']['global_denied_groups'][$test] && $allVals['cn'][0])
573                                        {
574                                                $accounts[] = Array(
575                                                        'account_id'        => $allVals['gidnumber'][0],
576                                                        'account_lid'       => $allVals['cn'][0],
577                                                        'account_type'      => $allVals['phpgwaccounttype'][0],
578                                                        'account_firstname' => $GLOBALS['phpgw']->translation->convert($allVals['givenname'][0],'utf-8'),
579                                                        'account_lastname'  => $GLOBALS['phpgw']->translation->convert($allVals['sn'][0],'utf-8'),
580                                                        'account_status'    => $allVals['phpgwaccountstatus'][0],
581                                                        'account_email'     => $allVals['mail'][0],
582                                                );
583                                        }
584                                }
585                        }
586                        // sort the array
587                        $arrayFunctions = CreateObject('phpgwapi.arrayfunctions');
588                        if(empty($order))
589                        {
590                                $order = 'account_lid';
591                        }
592                        $sortedAccounts = $arrayFunctions->arfsort($accounts,explode(',',$order),$sort);
593                        $this->total = count($accounts);
594
595                        // return only the wanted accounts
596
597                        if (is_array($sortedAccounts))
598                        {
599                                reset($sortedAccounts);
600                                if(is_int($start) && is_int($offset))
601                                {
602                                        return array_slice($sortedAccounts, $start, $offset);
603                                }
604                                elseif(is_int($start))
605                                {
606                                        return array_slice($sortedAccounts, $start, $GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs']);
607                                }
608                                else
609                                {
610                                        return $sortedAccounts;
611                                }
612                        }
613                        return False;
614                }
615
616                function name2id($name,$which='account_lid')
617                {
618                        if ($which == 'account_lid')    // groups only support account_lid
619                        {
620                                $sri = ldap_search($this->ds, $this->group_context, '(&(cn=' . (string)$name . ')(phpgwaccounttype=g))');
621                                $allValues = ldap_get_entries($this->ds, $sri);
622
623                                if (@$allValues[0]['gidnumber'][0])
624                                {
625                                        return (int)$allValues[0]['gidnumber'][0];
626                                }
627                        }
628                        $to_ldap = array(
629                                'account_lid'   => 'uid',
630                                'account_email' => 'mail',
631                        );
632                        if (!isset($to_ldap[$which])) return False;
633
634                        $sri = ldap_search($this->ds, $this->user_context, '(&('.$to_ldap[$which].'=' . (string)$name . ')(phpgwaccounttype=u))');
635
636                        $allValues = ldap_get_entries($this->ds, $sri);
637
638                        if (@$allValues[0]['uidnumber'][0])
639                        {
640                                return (int)$allValues[0]['uidnumber'][0];
641                        }
642
643                        return False;
644                }
645
646                function id2name($account_id,$which='account_lid')
647                {
648                        if ($which == 'account_lid' || $which == 'account_type')        // groups only support account_lid and account_type
649                        {
650                                $allValues = array();
651                                $sri = ldap_search($this->ds, $this->group_context, '(&(gidnumber=' . (int)$account_id . ')(phpgwaccounttype=g))');
652                                $allValues = ldap_get_entries($this->ds, $sri);
653
654                                $attr = $which == 'account_lid' ? 'cn' : 'phpgwaccounttype';
655                                if (@$allValues[0]['cn'][0])
656                                {
657                                        return $allValues[0]['cn'][0];
658                                }
659                        }
660                        $to_ldap = array(
661                                'account_lid'   => 'uid',
662                                'account_email' => 'mail',
663                                'account_firstname' => 'surname',
664                                'account_lastname'  => 'cn',
665                                'account_type'      => 'phpgwaccounttype',
666                        );
667                        if (!isset($to_ldap[$which])) return False;
668
669                        $allValues = array();
670                        $sri = ldap_search($this->ds, $this->user_context, '(&(uidnumber=' . (int)$account_id . ')(phpgwaccounttype=u))');
671                        $allValues = ldap_get_entries($this->ds, $sri);
672
673                        if (@$allValues[0][$to_ldap[$which]][0])
674                        {
675                                return $allValues[0][$to_ldap[$which]][0];
676                        }
677                        return False;
678                }
679
680                function get_type($account_id)
681                {
682                        $allValues = array();
683                        $sri = ldap_search($this->ds, $this->user_context, '(&(uidnumber=' . (int)$account_id . ')(phpgwaccounttype=u))');
684                        $allValues = ldap_get_entries($this->ds, $sri);
685
686                        if ($allValues[0]['phpgwaccounttype'][0])
687                        {
688                                return $allValues[0]['phpgwaccounttype'][0];
689                        }
690
691                        $allValues = array();
692                        $sri = ldap_search($this->ds, $this->group_context, '(&(gidnumber=' . (int)$account_id . ')(phpgwaccounttype=g))');
693                        $allValues = ldap_get_entries($this->ds, $sri);
694
695                        if ($allValues[0]['phpgwaccounttype'][0])
696                        {
697                                return $allValues[0]['phpgwaccounttype'][0];
698                        }
699                        return False;
700                }
701
702                /*
703                 * returns nonzero if $account exists in LDAP: 0: nowhere 1: user accounts, 2: group accounts, 3: both
704                 * $account can be an account_id (LDAP: uidnumber) or an account_lid (LDAP: uid) (is determinded by ettype($account) == 'integer')
705                 */
706                function exists($account)
707                {
708                        /* This sets up internal caching variables for this functon */
709                        static $by_id, $by_lid;
710                        $users  = array();
711                        $groups = array();
712
713                        if(is_int($account))
714                        {
715                                $ldapgroup = 'gidnumber';
716                                $ldapacct  = 'uidnumber';
717                                $account   = (int)$account;
718                                /* If data is cached, use it. */
719                                if(@isset($by_id[$account]) && @$by_id[$account])
720                                {
721                                        return $by_id[$account];
722                                }
723                        }
724                        else
725                        {
726                                $ldapgroup = 'cn';
727                                $ldapacct  = 'uid';
728                                /* If data is cached, use it. */
729                                if(@isset($by_lid[$account]) && @$by_lid[$account])
730                                {
731                                        return $by_lid[$account];
732                                }
733                        }
734
735                        $acct_type = $this->get_type($account);
736
737                        if ($acct_type == 'g' && $this->group_context)
738                        {
739                                $sri = ldap_search($this->ds, $this->group_context, $ldapgroup . '=' . $account);
740                                $groups = ldap_get_entries($this->ds, $sri);
741                        }
742                        $sri = ldap_search($this->ds, $this->user_context, $ldapacct . '=' . $account);
743                        $users = ldap_get_entries($this->ds, $sri);
744
745                        if ($users[0]['dn'])
746                        {
747                                $in += 1;
748                        }
749                        if ($groups[0]['dn'])
750                        {
751                                $in += 2;
752                        }
753                        /* This sets up internal caching for this function */
754                        if($ldapgroup == 'gidnumber')
755                        {
756                                $by_id[$account] = $in;
757                                $by_lid[$this->id2name($account)] = $in;
758                        }
759                        else
760                        {
761                                $by_lid[$account] = $in;
762                                $by_id[$this->name2id($account)] = $in;
763                        }
764
765                        return $in;
766                }
767
768                function create($account_info,$default_prefs=True)
769                {
770                        /* echo '<br>in create for account_lid: "'.$account_lid.'"'; */
771                        if (empty($account_info['account_id']) || !$account_info['account_id'])
772                        {
773                                $account_id = $this->get_nextid($account_info['account_type']);
774                                /* echo '<br>using'.$account_id;exit; */
775                        }
776                        else
777                        {
778                                $account_id = $account_info['account_id'];
779                        }
780                        $entry['userpassword']              = $account_info['account_passwd'];
781                        $entry['phpgwaccounttype']          = $account_info['account_type'];
782                        //$entry[$GLOBALS['phpgw_info']['server']['atributoexpiracao']] = $account_info['account_expires'];
783                        //$entry['phpgwaccountexpires']       = $account_info['account_expires'];
784                        if(isset($GLOBALS['phpgw_info']['server']['atributoexpiracao']))
785                                        {
786                                        $entry[$GLOBALS['phpgw_info']['server']['atributoexpiracao']] = $account_info['account_expires'];
787                                        }
788                                else
789                                        {
790                                        $entry['phpgwaccountexpires'] = $account_info['account_expires'];
791                                        }
792                        if($account_info['account_type'] == 'g')
793                        {
794                                $sri = ldap_search($this->ds, $this->group_context, 'cn=' . (string)$account_info['account_lid']);
795                        }
796                        else
797                        {
798                                $sri = ldap_search($this->ds, $this->user_context, 'uid=' . (string)$account_info['account_lid']);
799                        }
800                        $allValues = ldap_get_entries($this->ds, $sri);
801
802                        if ($GLOBALS['phpgw_info']['server']['ldap_extra_attributes'] && $account_info['account_type'] != 'g')
803                        {
804                                $entry['homedirectory'] = $account_info['homedirectory'] && $account_info['homedirectory'] != $GLOBALS['phpgw_info']['server']['ldap_account_home'] ? $account_info['homedirectory'] : $GLOBALS['phpgw_info']['server']['ldap_account_home'].SEP.$account_info['account_lid'];
805                                $entry['loginshell'] = $account_info['loginshell'] ? $account_info['loginshell'] : $GLOBALS['phpgw_info']['server']['ldap_account_shell'];
806                        }
807                        elseif($account_info['account_type'] != 'g')
808                        {
809                                $entry['homedirectory'] = '/home/'.$account_info['account_lid'];
810                                $entry['loginshell'] = '/bin/false';
811                        }
812
813                        if ($allValues[0]['dn'])
814                        {
815                                /* This should keep the password from being overwritten here on ldap import */
816                                unset($entry['userpassword']);
817                                $entry['gidnumber'] = $account_id;
818
819                                while (list($key,$val) = each($entry))
820                                {
821                                        $tmpentry = '';
822                                        $tmpentry[$key] = trim($val); /* must trim! */
823                                        /* echo '<br>'.$key.' '.$val; */
824                                        if ($tmpentry[$key])
825                                        {
826                                                if (!$allValues[0][$key][0])
827                                                {
828                                                        /* attribute was not in LDAP, add it */
829                                                        ldap_mod_add($this->ds, $allValues[0]['dn'], $tmpentry);
830                                                }
831                                                else
832                                                {
833                                                        /* attribute was in LDAP, modify it */
834                                                        ldap_modify($this->ds, $allValues[0]['dn'], $tmpentry);
835                                                }
836                                        }
837                                }
838
839                                if ($account_info['account_type'] == 'g')
840                                {
841                                        $tmpentry['objectclass'][0] = 'top';
842                                        $tmpentry['objectclass'][1] = 'posixGroup';
843                                        $tmpentry['objectclass'][2] = 'phpgwAccount';
844                                }
845                                else
846                                {
847                                        $tmpentry['uidnumber']      = $account_id;
848                                        $tmpentry['objectclass'][0] = 'top';
849                                        $tmpentry['objectclass'][1] = 'person';
850                                        $tmpentry['objectclass'][2] = 'organizationalPerson';
851                                        $tmpentry['objectclass'][3] = 'inetOrgPerson';
852                                        $tmpentry['userpassword']   = $GLOBALS['phpgw']->common->encrypt_password($account_info['account_passwd'],False);
853                                        /* $tmpentry['objectclass'][4] = 'account'; Causes problems with some LDAP servers */
854                                        $tmpentry['objectclass'][4] = 'posixAccount';
855                                        $tmpentry['objectclass'][5] = 'shadowAccount';
856                                        $tmpentry['objectclass'][6] = 'phpgwAccount';
857                                        $tmpentry['phpgwaccountstatus']    = $account_info['account_status'];
858                                        $tmpentry['phpgwaccounttype']      = $account_info['account_type'];
859                                        //$tmpentry['phpgwaccountexpires']   = $account_info['account_expires'];
860                                        //$tmpentry[$GLOBALS['phpgw_info']['server']['atributoexpiracao']]   = $account_info['account_expires'];
861                                        if(isset($GLOBALS['phpgw_info']['server']['atributoexpiracao']))
862                                                        {
863                                                        $tmpentry[$GLOBALS['phpgw_info']['server']['atributoexpiracao']] = $account_info['account_expires'];
864                                                        }
865                                                else
866                                                        {
867                                                        $tmpentry['phpgwaccountexpires'] = $account_info['account_expires'];
868                                                        }
869                                }
870                                ldap_modify($this->ds, $allValues[0]['dn'], $tmpentry);
871                        }
872                        else
873                        {
874                                /* Not already there, we will add it */
875                                if ($account_info['account_type'] == 'g')
876                                {
877                                        $dn = 'cn='.$account_info['account_lid'] . ',' . $this->group_context;
878                                        unset($entry['homedirectory']);
879                                        unset($entry['loginshell']);
880                                        $entry['objectclass'][0] = 'top';
881                                        $entry['objectclass'][1] = 'posixGroup';
882                                        $entry['objectclass'][2] = 'phpgwAccount';
883                                        $entry['cn']             = $GLOBALS['phpgw']->translation->convert($account_info['account_lid'],$GLOBALS['phpgw']->translation->charset(),'utf-8');
884                                        $entry['gidnumber']      = $account_id;
885                                        #$entry['userpassword']   = $GLOBALS['phpgw']->common->encrypt_password($account_info['account_passwd']);
886                                        $entry['description']    = 'phpgw-created group';
887                                }
888                                else
889                                {
890                                        $dn = 'uid=' . $account_info['account_lid'] . ',' . $this->user_context;
891
892                                        $entry['cn'] = $GLOBALS['phpgw']->translation->convert(
893                                                sprintf(
894                                                        "%s %s",
895                                                        $account_info['account_firstname'],
896                                                        $account_info['account_lastname']
897                                                ),
898                                                $GLOBALS['phpgw']->translation->charset(),
899                                                'utf-8'
900                                        );
901
902                                        $entry['sn'] = $GLOBALS['phpgw']->translation->convert(
903                                                $account_info['account_lastname'],
904                                                $GLOBALS['phpgw']->translation->charset(),
905                                                'utf-8'
906                                        );
907
908                                        if($account_info['account_firstname'])
909                                        {
910                                                $entry['givenname'] = $GLOBALS['phpgw']->translation->convert(
911                                                        $account_info['account_firstname'],
912                                                        $GLOBALS['phpgw']->translation->charset(),
913                                                        'utf-8'
914                                                );
915                                        }
916                                        if($account_info['account_email'])
917                                        {
918                                                $entry['mail'] = $GLOBALS['phpgw']->translation->convert(
919                                                        $account_info['account_email'],
920                                                        $GLOBALS['phpgw']->translation->charset(),
921                                                        'utf-8'
922                                                );
923                                        }
924                                        $entry['uid']            = $account_info['account_lid'];
925                                        $entry['uidnumber']      = $account_id;
926                                        $entry['gidnumber']      = $account_info['account_primary_group'];
927                                        $entry['userpassword']   = $GLOBALS['phpgw']->common->encrypt_password($account_info['account_passwd']);
928                                        $entry['objectclass'][0] = 'top';
929                                        $entry['objectclass'][1] = 'person';
930                                        $entry['objectclass'][2] = 'organizationalPerson';
931                                        $entry['objectclass'][3] = 'inetOrgPerson';
932                                        $entry['objectclass'][4] = 'posixAccount';
933                                        $entry['objectclass'][5] = 'shadowAccount';
934                                        $entry['objectclass'][6] = 'phpgwAccount';
935                                        if($account_info['account_status'])
936                                        {
937                                                $entry['phpgwaccountstatus']    = $account_info['account_status'];
938                                        }
939                                        $entry['phpgwaccounttype']      = $account_info['account_type'];
940                                        //$entry['phpgwaccountexpires']   = $account_info['account_expires'];
941                                        //$entry[$GLOBALS['phpgw_info']['server']['atributoexpiracao']]   = $account_info['account_expires'];
942                                        if(isset($GLOBALS['phpgw_info']['server']['atributoexpiracao']))
943                                                        {
944                                                        $entry[$GLOBALS['phpgw_info']['server']['atributoexpiracao']] = $account_info['account_expires'];
945                                                        }
946                                                else
947                                                        {
948                                                        $entry['phpgwaccountexpires'] = $account_info['account_expires'];
949                                                        }
950                                }
951                                #_debug_array($entry);
952                                // stop processing if ldap_add fails
953                                if(!ldap_add($this->ds, $dn, $entry))
954                                {
955                                        return false;
956                                }
957                        }
958                        // print ldap_error($this->ds);
959
960                        // lets check group the user needs to be added
961                        if($account_info['account_type'] == 'u')
962                        {
963                                @settype($account_info['account_groups'],'array');
964                                foreach($account_info['account_groups'] as $key => $value)
965                                {
966                                        // search for the group
967                                        $filter    = 'gidnumber=' . (int)$value;
968                                        $justThese = array('memberuid');
969                                        $sri = ldap_search($this->ds, $this->group_context, $filter, $justThese);
970                                        if($sri)
971                                        {
972                                                $allValues = ldap_get_entries($this->ds, $sri);
973                                                // if the user is not member of this group, add him
974                                                if(is_array($allValues[0]['memberuid']))
975                                                {
976                                                        // this group has already some members
977                                                        if(!in_array($account_info['account_lid'],$allValues[0]['memberuid']))
978                                                        {
979                                                                $dn = $allValues[0]['dn'];
980                                                                $newData = array();
981                                                                $newData['memberuid'] = $allValues[0]['memberuid'];
982                                                                unset($newData['memberuid']['count']);
983                                                                $newData['memberuid'][] = $account_info['account_lid'];
984                                                                $newData['memberuid'] = array_unique($newData['memberuid']);
985                                                                ldap_mod_replace($this->ds, $dn, $newData);
986                                                                #print ldap_error($this->ds)."<br>";
987                                                        }
988                                                }
989                                                else
990                                                {
991                                                        // this group has no members
992                                                        $dn = $allValues[0]['dn'];
993                                                        $newData = array();
994                                                        $newData['memberuid'][] = $account_info['account_lid'];
995                                                        ldap_mod_replace($this->ds, $dn, $newData);
996                                                }
997                                        }
998                                }
999                        }
1000
1001                        if($account_id && is_object($GLOBALS['phpgw']->preferences) && $default_prefs)
1002                        {
1003                                $GLOBALS['phpgw']->preferences->create_defaults($account_id);
1004                        }
1005
1006                        return $account_id;
1007                }
1008
1009                function auto_add($accountname, $passwd, $default_prefs = False, $default_acls = False, $expiredate = 0, $account_status = 'A')
1010                {
1011                        if ($expiredate == 0)
1012                        {
1013                                if(isset($GLOBALS['phpgw_info']['server']['auto_create_expire']) == True)
1014                                {
1015                                        if($GLOBALS['phpgw_info']['server']['auto_create_expire'] == 'never')
1016                                        {
1017                                                $expires = -1;
1018                                        }
1019                                        else
1020                                        {
1021                                                $expiredate = time() + $GLOBALS['phpgw_info']['server']['auto_create_expire'];
1022                                        }
1023                                }
1024                        }
1025                        else
1026                        {
1027                                /* expire in 30 days by default */
1028                                $expiredate = time() + ((60 * 60) * (30 * 24));
1029                        }
1030
1031                        if ($expires != -1)
1032                        {
1033                                $expires = mktime(2,0,0,date('n',$expiredate), (int)date('d',$expiredate), date('Y',$expiredate));
1034                        }
1035
1036                        $default_group_id  = $this->name2id($GLOBALS['phpgw_info']['server']['default_group_lid']);
1037                        if (!$default_group_id)
1038                        {
1039                                $default_group_id = (int) $this->name2id('Default');
1040                        }
1041                        $primary_group = $GLOBALS['auto_create_acct']['primary_group'] &&
1042                                $this->get_type((int)$GLOBALS['auto_create_acct']['primary_group']) == 'g' ?
1043                                (int) $GLOBALS['auto_create_acct']['primary_group'] : $default_group_id;
1044
1045                        $acct_info = array(
1046                                'account_lid'       => $accountname,
1047                                'account_type'      => 'u',
1048                                'account_passwd'    => $passwd,
1049                                'account_firstname' => $GLOBALS['auto_create_acct']['firstname'] ? $GLOBALS['auto_create_acct']['firstname'] : 'New',
1050                                'account_lastname'  => $GLOBALS['auto_create_acct']['lastname'] ? $GLOBALS['auto_create_acct']['lastname'] : 'User',
1051                                'account_status'    => $account_status,
1052                                'account_expires'   => $expires,
1053                                'account_primary_group' => $primary_group,
1054                        );
1055
1056                        /* attempt to set an email address */
1057                        if (isset($GLOBALS['auto_create_acct']['email']) == True && $GLOBALS['auto_create_acct']['email'] != '')
1058                        {
1059                                $acct_info['account_email'] = $GLOBALS['auto_create_acct']['email'];
1060                        }
1061                        elseif(isset($GLOBALS['phpgw_info']['server']['mail_suffix']) == True && $GLOBALS['phpgw_info']['server']['mail_suffix'] != '')
1062                        {
1063                                $acct_info['account_email'] = $accountname . '@' . $GLOBALS['phpgw_info']['server']['mail_suffix'];
1064                        }
1065
1066                        $this->db->transaction_begin();
1067
1068                        $this->create($acct_info,$default_prefs);  /* create the account */
1069
1070                        $accountid = $this->name2id($accountname); /* grab the account id or an error code */
1071
1072                        if ($accountid) /* begin account setup */
1073                        {
1074                                if($primary_group)
1075                                {
1076                                        $this->db->query("INSERT INTO phpgw_acl (acl_appname, acl_location, acl_account, acl_rights) VALUES('phpgw_group', "
1077                                                . $primary_group . ", " . $accountid . ", 1)",__LINE__,__FILE__);
1078                                }
1079
1080                                /* FIXME - we are assuming the auth method is capable of password changing
1081                                 * $this->db->query("INSERT INTO phpgw_acl (acl_appname, acl_location, acl_account, acl_rights)VALUES('preferences', 'changepassword', ".$accountid.", 1)",__LINE__,__FILE__);
1082                                 */
1083
1084                                /* if we have an mail address set it in the uesrs' email preference */
1085                                if (isset($GLOBALS['auto_create_acct']['email']) && $GLOBALS['auto_create_acct']['email'] != '')
1086                                {
1087                                        $GLOBALS['phpgw']->acl->acl($accountid);        /* needed als preferences::save_repository calls acl */
1088                                        $GLOBALS['phpgw']->preferences->preferences($accountid);
1089                                        $GLOBALS['phpgw']->preferences->read_repository();
1090                                        $GLOBALS['phpgw']->preferences->add('email','address',$GLOBALS['auto_create_acct']['email']);
1091                                        $GLOBALS['phpgw']->preferences->save_repository();
1092                                }
1093                                /* use the default mail domain to set the uesrs' email preference  */
1094                                elseif(isset($GLOBALS['phpgw_info']['server']['mail_suffix']) && $GLOBALS['phpgw_info']['server']['mail_suffix'] != '')
1095                                {
1096                                        $GLOBALS['phpgw']->acl->acl($accountid);        /* needed als preferences::save_repository calls acl */
1097                                        $GLOBALS['phpgw']->preferences->preferences($accountid);
1098                                        $GLOBALS['phpgw']->preferences->read_repository();
1099                                        $GLOBALS['phpgw']->preferences->add('email','address', $accountname . '@' . $GLOBALS['phpgw_info']['server']['mail_suffix']);
1100                                        $GLOBALS['phpgw']->preferences->save_repository();
1101                                }
1102
1103                                /* commit the new account transaction */
1104                                $this->db->transaction_commit();
1105
1106                                /* does anyone know what the heck this is required for? */
1107                                $GLOBALS['hook_values']['account_lid']  = $acct_info['account_lid'];
1108                                $GLOBALS['hook_values']['account_id']   = $accountid;
1109                                $GLOBALS['hook_values']['new_passwd']   = $acct_info['account_passwd'];
1110                                $GLOBALS['hook_values']['account_status'] = $acct_info['account_status'];
1111                                $GLOBALS['hook_values']['account_firstname'] = $acct_info['account_firstname'];
1112                                $GLOBALS['hook_values']['account_lastname'] = $acct_info['account_lastname'];
1113                                $GLOBALS['phpgw']->hooks->process($GLOBALS['hook_values']+array(
1114                                        'location' => 'addaccount'
1115                                ),False,True);  // called for every app now, not only enabled ones
1116
1117                        } /* end account setup */
1118                        else /* if no account id abort the account creation */
1119                        {
1120                                $this->db->transaction_abort();
1121                        }
1122
1123                        /*
1124                         * If we succeeded in creating the account (above), return the accountid, else,
1125                         * return the error value from $this->name2id($accountname)
1126                         */
1127                        return $accountid;
1128                } /* end auto_add() */
1129
1130                function get_account_name($account_id,&$lid,&$fname,&$lname)
1131                {
1132                        $acct_type = $this->get_type($account_id);
1133
1134                        /* search the dn for the given uid */
1135                        if(($acct_type == 'g') && $this->group_context)
1136                        {
1137                                $sri = ldap_search($this->ds, $this->group_context, '(&(gidnumber=' . (int)$account_id . ')(phpgwAccountType=g))');
1138                        }
1139                        else
1140                        {
1141                                $sri = ldap_search($this->ds, $this->user_context, 'uidnumber=' . (int)$account_id);
1142                        }
1143                        $allValues = ldap_get_entries($this->ds, $sri);
1144
1145                        if($acct_type =='g')
1146                        {
1147                                $lid   = $GLOBALS['phpgw']->translation->convert($allValues[0]['cn'][0],'utf-8');
1148                                $fname = $GLOBALS['phpgw']->translation->convert($allValues[0]['cn'][0],'utf-8');
1149                                $lname = lang('Group');
1150                        }
1151                        else
1152                        {
1153                                $lid   = $GLOBALS['phpgw']->translation->convert($allValues[0]['uid'][0],'utf-8');
1154                                $fname = $GLOBALS['phpgw']->translation->convert($allValues[0]['givenname'][0],'utf-8');
1155                                $lname = $GLOBALS['phpgw']->translation->convert($allValues[0]['sn'][0],'utf-8');
1156                        }
1157                        return !empty($lid);
1158                }
1159
1160                function getDNforID($_accountid = '')
1161                {
1162                        $_account_id = get_account_id($_accountid);
1163
1164                        $sri = ldap_search($this->ds, $this->user_context, 'uidnumber=' . (int)$_account_id);
1165                        $allValues = ldap_get_entries($this->ds, $sri);
1166
1167                        return $allValues[0]['dn'];
1168                }
1169
1170                //by JakJr, add organization support
1171                function get_context($dn)
1172                {
1173                        return substr($dn, (strpos($dn, ',')+1));
1174                }
1175
1176                //by JakJr, add organization support
1177                function get_organization($dn)
1178                {
1179                        $ldap_context = $GLOBALS['phpgw_info']['server']['ldap_context'];
1180                        $end = (strpos($dn, $ldap_context)-1);
1181                        $tmp =  substr($dn, 0, $end);
1182                        $begin = (strrpos($tmp, '=')+1);
1183                        return substr($dn, $begin, ($end - $begin));
1184                }
1185
1186                //by JakJr, add organization support
1187                function get_sector($dn)
1188                {
1189                        $context = $this->get_context($dn);
1190                        $begin = (strpos($context, '=')+1);
1191                        $end = (strpos($context, ',') - $begin);
1192                        return substr($context, $begin, $end);
1193                }
1194
1195                function id2fullname($account_id)
1196                {
1197                        $allValues = array();
1198                        $sri = ldap_search($this->ds, $this->user_context, '(&(uidnumber=' . (int)$account_id . ')(phpgwaccounttype=u))');
1199                        $allValues = ldap_get_entries($this->ds, $sri);
1200                        if (@$allValues[0]['cn'][0])
1201                        {
1202                                return $allValues[0]['cn'][0];
1203                        }
1204                        return False;
1205               }
1206
1207                function mail2fullname($mail)
1208                {
1209                        $allValues = array();
1210                        $sri = ldap_search($this->ds, $this->user_context, '(&(mail=' . $mail . ')(phpgwaccounttype=u))');
1211                        $allValues = ldap_get_entries($this->ds, $sri);
1212                        if (@$allValues[0]['cn'][0])
1213                        {
1214                                return $allValues[0]['cn'][0];
1215                        }
1216                        return False;
1217                }
1218                //Usada para fazer rebind em caso de escrita em slave (quando volta uma referral)
1219        }
1220?>
Note: See TracBrowser for help on using the repository browser.