source: trunk/expressoAdmin1_2/inc/class.ldap_functions.inc.php @ 1520

Revision 1520, 77.0 KB checked in by eduardoalex, 15 years ago (diff)

Ticket #685 - Add algoritimo de login e correção na geração de logins

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2define('PHPGW_INCLUDE_ROOT','../');
3define('PHPGW_API_INC','../phpgwapi/inc');     
4include_once(PHPGW_API_INC.'/class.common.inc.php');
5include_once('class.functions.inc.php');
6
7function ldapRebind($ldap_connection, $ldap_url)
8{
9        // Enquanto estivermos utilizando referral na arvore ldap, teremos que continuar a utilizar o usuário sistemas:expresso.
10        // Depois, quando não existir mais referral, não existirá a necessidade de ldapRebind.
11        //ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_master_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_master_root_pw']);
12        if ( ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
13        {
14                @ldap_bind($ldap_connection, $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'], $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw']);
15        }
16}
17
18class ldap_functions
19{
20        var $ldap;
21        var $current_config;
22        var $functions;
23        var $manager_contexts;
24       
25        function ldap_functions(){             
26                $GLOBALS['phpgw_info']['server'] = $_SESSION['phpgw_info']['expresso']['server'];
27                $this->current_config = $_SESSION['phpgw_info']['expresso']['expressoAdmin'];
28                $common = new common();
29               
30                if ( (!empty($GLOBALS['phpgw_info']['server']['ldap_master_host'])) &&
31                         (!empty($GLOBALS['phpgw_info']['server']['ldap_master_root_dn'])) &&
32                         (!empty($GLOBALS['phpgw_info']['server']['ldap_master_root_pw'])) )
33                {
34                        $this->ldap = $common->ldapConnect($GLOBALS['phpgw_info']['server']['ldap_master_host'],
35                                                                                           $GLOBALS['phpgw_info']['server']['ldap_master_root_dn'],
36                                                                                           $GLOBALS['phpgw_info']['server']['ldap_master_root_pw']);
37                }
38                else
39                {
40                        $this->ldap = $common->ldapConnect();
41                }
42               
43                $this->functions = new functions;
44                $manager_acl = $this->functions->read_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid']);
45                $this->manager_contexts = $manager_acl['contexts'];
46        }
47       
48        /* expressoAdmin: email lists : deve utilizar o ldap Host Master com o usuario e senha do CC*/
49        /* ldap connection following referrals and using Master config, from setup */
50        function ldapMasterConnect()
51        {
52                /*
53                $common = new common();
54                if ( (!empty($GLOBALS['phpgw_info']['server']['ldap_master_host'])) &&
55                         (!empty($GLOBALS['phpgw_info']['server']['ldap_master_root_dn'])) &&
56                         (!empty($GLOBALS['phpgw_info']['server']['ldap_master_root_pw'])) )
57                {
58                        $ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_master_host']);
59                        ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
60                        ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
61                        ldap_set_rebind_proc($ldap_connection, ldapRebind);
62                        ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_master_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_master_root_pw']);
63                }
64                else
65                {
66                        $ldap_connection = $common->ldapConnect($GLOBALS['phpgw_info']['server']['ldap_host'],
67                                                                                           $GLOBALS['phpgw_info']['server']['ldap_root_dn'],
68                                                                                           $GLOBALS['phpgw_info']['server']['ldap_root_pw'], true);
69                }
70               
71                // If success, return follow_referral connection. Else, return normal connection.
72                if ($ldap_connection)
73                        return $ldap_connection;
74                else
75                        return $this->ldap;
76                */
77               
78                // Este if é para utilizar o master. (para replicação)
79                if ( (!empty($GLOBALS['phpgw_info']['server']['ldap_master_host'])) && ($ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_master_host'])) )
80                {
81                        ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
82                        ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
83                        ldap_set_rebind_proc($ldap_connection, ldapRebind);
84                        if ( ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
85                        {
86                                if ( ! ldap_bind($ldap_connection, $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'], $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw']) )
87                                {
88                                        return false;
89                                }
90                        }
91                        return $ldap_connection;
92                }
93                else
94                {
95                        $ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
96                        if ($ldap_connection)
97                        {
98                                ldap_set_option($ldap_connection,LDAP_OPT_PROTOCOL_VERSION,3);
99                                ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
100                                if ( ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']) )
101                                        return $ldap_connection;
102                        }
103                }
104               
105                return false;
106        }
107               
108        function validate_fields($params)
109        {
110                /* ldap connection following referals and using Contac Center config*/
111                if (is_array($_SESSION['phpgw_info']['expresso']['cc_ldap_server']))
112                {
113                        $ldap_connection = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host']);
114                        if ($ldap_connection)
115                        {
116                                ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
117                                ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
118                               
119                                if ( ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
120                                        ldap_bind($ldap_connection, $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'], $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw']);
121                                $context = $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['dn'];
122                        }
123                        else
124                        {
125                                $result['status'] = false;
126                                $result['msg'] = $this->functions->lang('Connection with ldap fail') . ".";
127                                return $result;
128                        }
129                }
130                else
131                {
132                        $ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
133                        ldap_set_option($ldap_connection,LDAP_OPT_PROTOCOL_VERSION,3);
134                        ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
135                        ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']);
136                        $context = $GLOBALS['phpgw_info']['server']['ldap_context'];
137                }
138               
139                $result['status'] = true;
140               
141                $params = unserialize($params['attributes']);
142                $type = $params['type'];
143                $uid = $params['uid'];
144                $mail = $params['mail'];
145                $mailalternateaddress = $params['mailalternateaddress'];
146                $cpf = $params['cpf'];
147                               
148                if ($_SESSION['phpgw_info']['expresso']['global_denied_users'][$uid])
149                {
150                        $result['status'] = false;
151                        $result['msg'] = $this->functions->lang('this login can not be used because is a system account') . ".";
152                        return $result;
153                }
154               
155                if (($type == 'create_user') || ($type == 'rename_user'))
156                {
157                        if ($this->current_config['expressoAdmin_prefix_org'] == 'true')
158                        {
159                                //Obtenho UID sem a organização. Na criação o uid já vem sem a organização
160                                $tmp_uid_without_org = split("-", $params['uid']);
161                                $tmp_reverse_uid_without_org = array_reverse($tmp_uid_without_org);
162                                array_pop($tmp_reverse_uid_without_org);
163                                $uid_without_org = implode("-", $tmp_reverse_uid_without_org);
164                                $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|(uid=$uid)(uid=$uid_without_org)))";
165                        }
166                        else
167                        {
168                                $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uid=$uid))";
169                        }
170                        /*
171                        //UID
172                        if (($type == 'rename_user') && ($this->current_config['expressoAdmin_prefix_org'] == 'true'))
173                        {
174                                //Obtenho UID sem a organização. Na criação o uid já vem sem a organização
175                                $tmp_uid_without_org = split("-", $params['uid']);
176                                $tmp_reverse_uid_without_org = array_reverse($tmp_uid_without_org);
177                                array_pop($tmp_reverse_uid_without_org);
178                                $uid_without_org = implode("-", $tmp_reverse_uid_without_org);
179                                $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|(uid=$uid)(uid=$uid_without_org)))";
180                        }
181                        else
182                        {
183                                $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uid=$uid))";
184                        }
185                        */
186                       
187                        $justthese = array("uid", "mail", "cn");
188                        $search = ldap_search($ldap_connection, $context, $filter, $justthese);
189                        $count_entries = ldap_count_entries($ldap_connection,$search);
190                        if ($count_entries > 0)
191                        {
192                                $entries = ldap_get_entries($ldap_connection, $search);
193                               
194                                for ($i=0; $i<$entries['count']; $i++)
195                                {
196                                        $users .= $entries[$i]['cn'][0] . ' - ' . $entries[$i]['mail'][0] . "\n";
197                                }
198                               
199                                $result['status'] = false;
200                                $result['msg'] = $this->functions->lang('this login is already used by') . ":\n" . $users;
201                                return $result;
202                        }
203
204                        // GRUPOS
205                        $filter = "(&(phpgwAccountType=g)(cn=$uid))";
206                        $justthese = array("cn");
207                        $search = ldap_search($ldap_connection, $context, $filter, $justthese);
208                        $count_entries = ldap_count_entries($ldap_connection,$search);
209                        if ($count_entries > 0)
210                        {
211                                $result['status'] = false;
212                                $result['msg'] = $this->functions->lang('This login is being used by a group') . ".";
213                                return $result;
214                        }
215                       
216                       
217                        // UID em outras organizações, pesquiso apenas na maquina local e se utilizar prefix_org
218                        if ($this->current_config['expressoAdmin_prefix_org'] == 'true')
219                        {
220                                $ldap_connection2 = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
221                                ldap_set_option($ldap_connection2,LDAP_OPT_PROTOCOL_VERSION,3);
222                                ldap_set_option($ldap_connection2, LDAP_OPT_REFERRALS, false);
223                                ldap_bind($ldap_connection2, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']);
224                                $context = $GLOBALS['phpgw_info']['server']['ldap_context'];
225                               
226                                //Obtenho UID sem a organização
227                                /*
228                                $tmp_uid_without_org = split("-", $params['uid']);
229                                if (count($tmp_uid_without_org) < 2)
230                                {
231                                        $result['status'] = false;
232                                        $result['msg'] = 'Novo login sem organização.';
233                                        return $result;
234                                }
235                                $tmp_reverse_uid_without_org = array_reverse($tmp_uid_without_org);
236                                array_pop($tmp_reverse_uid_without_org);
237                                $uid_without_org = implode("-", $tmp_reverse_uid_without_org);
238                                */
239                               
240                                $filter = "(ou=*)";
241                                $justthese = array("ou");
242                                $search = ldap_list($ldap_connection2, $context, $filter, $justthese);
243                                $entries = ldap_get_entries($ldap_connection2   ,$search);
244                               
245                                foreach ($entries as $index=>$org)
246                                {
247                                        $organization = $org['ou'][0];
248                                        $organization = strtolower($organization);
249                               
250                                        $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uid=$organization-$uid))";
251                                       
252                                        $justthese = array("uid");
253                                        $search = ldap_search($ldap_connection2, $context, $filter, $justthese);
254                                        $count_entries = ldap_count_entries($ldap_connection2,$search);
255                                        if ($count_entries > 0)
256                                        {
257                                                $result['status'] = false;
258                                                $result['msg'] = $this->functions->lang('this login is already used by a user in another organization') . ".";
259                                                ldap_close($ldap_connection2);
260                                                return $result;
261                                        }
262                                }
263                                ldap_close($ldap_connection2);
264                        }
265                }
266               
267                if ($type == 'rename_user')
268                {
269                        return $result;
270                }
271               
272                // MAIL
273                $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|(mail=$mail)(mailalternateaddress=$mail)))";
274                $justthese = array("mail", "uid");
275                $search = ldap_search($ldap_connection, $context, $filter, $justthese);
276                $entries = ldap_get_entries($ldap_connection,$search);
277                if ($entries['count'] == 1){
278                        if ($entries[0]['uid'][0] != $uid){
279                                $result['status'] = false;
280                                $result['msg'] = $this->functions->lang('this email address is being used by 1 user') . ": " . $entries[0]['uid'][0];
281                                return $result;
282                        }
283                }
284                else if ($entries['count'] > 1){
285                        $result['status'] = false;
286                        $result['msg'] = $this->functions->lang('this email address is being used by 2 or more users') . ".";
287                        return $result;
288                }
289               
290                // MAILAlternateAddress
291                $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|(mail=$mailalternateaddress)(mailalternateaddress=$mailalternateaddress)))";
292                $justthese = array("mail", "uid");
293                $search = ldap_search($ldap_connection, $context, $filter, $justthese);
294                $entries = ldap_get_entries($ldap_connection,$search);
295                if ($entries['count'] == 1){
296                        if ($entries[0]['uid'][0] != $uid){
297                                $result['status'] = false;
298                                $result['msg'] = $this->functions->lang('alternative email is being used by 1 user') . ": " . $entries[0]['uid'][0];
299                                return $result;
300                        }
301                }
302                else if ($entries['count'] > 1){
303                        $result['status'] = false;
304                        $result['msg'] = $this->functions->lang('alternative email is being used by 2 or more users') . ".";
305                        return $result;
306                }
307
308                //Begin: Check CPF, only if the manager has access to this field.
309                if ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'manipulate_corporative_information'))
310                {
311                        if (!empty($cpf))
312                        {
313                                if (!$this->functions->checkCPF($cpf))
314                                {
315                                        $result['status'] = false;
316                                        $result['msg'] = $this->functions->lang('Field CPF is invalid') . '.';
317                                        return $result;
318                                }
319                                else
320                                {
321                                        //retira caracteres que não são números.
322                                        $cpf = ereg_replace("[^0-9]", "", $cpf);
323                               
324                                        $local_ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
325                                        if ($ldap_connection)
326                                        {
327                                                ldap_set_option($local_ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
328                                                ldap_set_option($local_ldap_connection, LDAP_OPT_REFERRALS, false);
329                                                ldap_bind($local_ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']);
330                                        }
331                                        else
332                                        {
333                                                $result['status'] = false;
334                                                $result['msg'] = $this->functions->lang('Connection with ldap fail') . ".";
335                                                return $result;
336                                        }
337                               
338                                        $filter = "(&(phpgwAccountType=u)(cpf=$cpf))";
339                                        $justthese = array("cn","uid");
340                                        $search = ldap_search($local_ldap_connection, $context, $filter, $justthese);
341                                        $entries = ldap_get_entries($local_ldap_connection,$search);
342                               
343                                        if ( ($entries['count'] != 1) && (strcasecmp($uid, $entries[0]['uid'][0]) != 0) )
344                                        {
345                                                if ($entries['count'] > 0)
346                                                {
347                                                        $result['question'] = $this->functions->lang('Field CPF used by') . ":\n";
348                                                        for ($i=0; $i<$entries['count']; $i++)
349                                                        {
350                                                                if (strcasecmp($uid, $entries[$i]['uid'][0]) != 0)
351                                                                        $result['question'] .= "- " . $entries[$i]['cn'][0] . "\n";
352                                                        }
353                                                        $result['question'] .= $this->functions->lang("Do you want to continue anyway") . "?";
354                                                        return $result;
355                                                }
356                                        }
357                                        ldap_close($local_ldap_connection);
358                                }
359                        }
360                        else if ($this->current_config['expressoAdmin_cpf_obligation'])
361                        {
362                                $result['status'] = false;
363                                $result['msg'] = $this->functions->lang('Field CPF must be completed') . '.';
364                                return $result;
365                        }
366                }
367                //End: Check CPF
368
369                return $result;
370        }
371       
372        function generate_login($params) {
373                $params = unserialize($params['attributes']);
374                $context = $GLOBALS['phpgw_info']['server']['ldap_context'];
375                $justthese = array("uid");
376                $i=1;
377                $login = array("status" => False,"msg" => $this->functions->lang("Login generator disabled"));
378                if( (isset($this->current_config['expressoAdmin_loginGenScript'])) &&
379                                ($this->current_config['expressoAdmin_loginGenScript'])) {
380                       
381                        include_once "if.login.inc.php";
382                        include_once "class.".$this->current_config['expressoAdmin_loginGenScript'].
383                                        ".inc.php";
384
385                        $classe = new ReflectionClass($this->current_config['expressoAdmin_loginGenScript']);
386                                       
387                        if(!$classe->implementsInterface('login'))
388                        {
389                                return array(
390                                        "status" => False,
391                                        "msg" => $this->functions->lang("Login interface not implemented (contact suport)")
392                                );
393                        }
394
395                        $login = $classe->newInstance()->generate_login($params["first_name"],$params["second_name"],$this->ldap);
396                       
397                        /*
398                                If login exists, it concatenates a number to the end.
399                                resulting in a new login
400                         */
401                        $i = 1;
402                        while($i < 1000) // Limit of 1000 equal names
403                        {
404                                $search = ldap_search($this->ldap, $context, "(uid=".$login.")", $justthese);
405                                if (ldap_count_entries($this->ldap,$search) == 0)
406                                        break;
407                                else
408                                {
409                                        if ($i > 1) // If login have a number, remove the number and put the new one
410                                                $login=substr($login,0,strlen($login)-strlen($i)).$i;
411                                        else
412                                                $login.=$i;
413                                        $i++;
414                                }
415                        }
416                }
417               
418                return array('status'=>true,'msg' => $login);
419        }
420        function validate_fields_group($params)
421        {
422                /* ldap connection following referals and using Contac Center config*/
423                if (is_array($_SESSION['phpgw_info']['expresso']['cc_ldap_server']))
424                {
425                        $ldap_connection = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host']);
426                        if ($ldap_connection)
427                        {
428                                ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
429                                ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
430                                if ( ($GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
431                                        ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['acc'], $GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['pw']);
432                                $context = $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['dn'];
433                        }
434                        else
435                        {
436                                $result['status'] = false;
437                                $result['msg'] = $this->functions->lang('Connection with ldap fail') . ".";
438                                return $result;
439                        }
440                }
441                else
442                {
443                        $ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
444                        ldap_set_option($ldap_connection,LDAP_OPT_PROTOCOL_VERSION,3);
445                        ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
446                        ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']);
447                        $context = $GLOBALS['phpgw_info']['server']['ldap_context'];
448                }
449
450                $cn = $params['cn'];
451                $result['status'] = true;
452               
453                if ($_SESSION['phpgw_info']['expresso']['global_denied_groups'][$cn])
454                {
455                        $result['status'] = false;
456                        $result['msg'] = $this->functions->lang('This group name can not be used because is a System Account') . ".";
457                        return $result;
458                }
459               
460                // CN
461                $filter = "(&(phpgwAccountType=g)(cn=$cn))";
462                $justthese = array("cn");
463                $search = ldap_search($ldap_connection, $context, $filter, $justthese);
464                $count_entries = ldap_count_entries($ldap_connection,$search);
465                if ($count_entries > 0)
466                {
467                        $result['status'] = false;
468                        $result['msg'] = $this->functions->lang('This name is already used') . ".";
469                        return $result;
470                }
471               
472                // UID
473                $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uid=$cn))";
474                $justthese = array("uid");
475                $search = ldap_search($ldap_connection, $context, $filter, $justthese);
476                $count_entries = ldap_count_entries($ldap_connection,$search);
477                if ($count_entries > 0)
478                {
479                        $result['status'] = false;
480                        $result['msg'] = $this->functions->lang('This grupo name is already used by an user') . ".";
481                        return $result;
482                }
483               
484                return $result;
485        }
486       
487        function validate_fields_maillist($params)
488        {
489                /* ldap connection following referals and using Contac Center config*/
490                if (is_array($_SESSION['phpgw_info']['expresso']['cc_ldap_server']))
491                {
492                        $ldap_connection = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host']);
493                        if ($ldap_connection)
494                        {
495                                ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
496                                ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
497                                if ( ($GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
498                                        ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['acc'], $GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['pw']);
499                                $context = $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['dn'];
500                        }
501                        else
502                        {
503                                $result['status'] = false;
504                                $result['msg'] = $this->functions->lang('Connection with ldap fail') . ".";
505                                return $result;
506                        }
507                }
508                else
509                {
510                        $ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
511                        ldap_set_option($ldap_connection,LDAP_OPT_PROTOCOL_VERSION,3);
512                        ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
513                        ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']);
514                        $context = $GLOBALS['phpgw_info']['server']['ldap_context'];
515                }
516               
517                $uid = $params['uid'];
518                $mail = $params['mail'];
519                $result['status'] = true;
520               
521                if ($_SESSION['phpgw_info']['expresso']['global_denied_users'][$uid])
522                {
523                        $result['status'] = false;
524                        $result['msg'] = $this->functions->lang('This LOGIN can not be used because is a System Account') . ".";
525                        return $result;
526                }
527               
528                // UID
529                $filter = "(&(phpgwAccountType=l)(uid=$uid))";
530                $justthese = array("uid");
531                $search = ldap_search($ldap_connection, $context, $filter, $justthese);
532                $count_entries = ldap_count_entries($ldap_connection,$search);
533                if ($count_entries > 0)
534                {
535                        $result['status'] = false;
536                        $result['msg'] = $this->functions->lang('this email list login is already used') . ".";
537                        return $result;
538                }
539               
540                // MAIL
541                $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|(mail=$mail)(mailalternateaddress=$mail)))";
542                $justthese = array("mail");
543                $search = ldap_search($ldap_connection, $context, $filter, $justthese);
544                $count_entries = ldap_count_entries($ldap_connection,$search);
545                if ($count_entries > 0)
546                {
547                        $result['status'] = false;
548                        $result['msg'] = $this->functions->lang('this email address is already used') . ".";
549                        return $result;
550                }
551               
552                return $result;
553        }
554
555        //Busca usuários de um contexto e já retorna as options do select;
556        function get_available_users($params)
557        {
558                $context = $params['context'];
559                $recursive = $params['recursive'];
560                $justthese = array("cn", "uidNumber");
561                $filter="(phpgwAccountType=u)";
562               
563                if ($recursive == 'true')
564                        $groups_list=ldap_search($this->ldap, $context, $filter, $justthese);
565                else
566                $groups_list=ldap_list($this->ldap, $context, $filter, $justthese);
567       
568        $entries = ldap_get_entries($this->ldap, $groups_list);
569       
570                for ($i=0; $i<$entries["count"]; $i++){
571                        $u_tmp[$entries[$i]["uidnumber"][0]] = $entries[$i]["cn"][0];
572                }
573                       
574                if (count($u_tmp))
575                        natcasesort($u_tmp);
576
577                $i = 0;
578                $users = array();
579                       
580                if (count($u_tmp))
581                {
582                        foreach ($u_tmp as $uidnumber => $cn)
583                        {
584                                $options .= "<option value=$uidnumber>$cn</option>";
585                        }
586                        unset($u_tmp);
587                }
588
589        return $options;
590        }
591
592        //Busca usuários e listas de um contexto e já retorna as options do select;
593        function get_available_users_and_maillist($params)
594        {
595                $context = $params['context'];
596                $recursive = $params['recursive'];
597               
598                //Usado para retirar a própria lista das possibilidades de inclusão.
599                $denied_uidnumber = $params['denied_uidnumber'];
600               
601                $justthese = array("cn", "uidNumber", "mail");
602               
603                $users_filter="(phpgwAccountType=u)";
604                $lists_filter = $denied_uidnumber == '' ? "(phpgwAccountType=l)" : "(&(phpgwAccountType=l)(!(uidnumber=$denied_uidnumber)))";
605               
606                $users = Array();
607                $lists = Array();               
608
609                /* folling referral connection */
610                $ldap_conn_following_ref = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host']);
611                if ($ldap_conn_following_ref)
612                {
613                        ldap_set_option($ldap_conn_following_ref, LDAP_OPT_PROTOCOL_VERSION, 3);
614                        ldap_set_option($ldap_conn_following_ref, LDAP_OPT_REFERRALS, 1);
615
616                        if ( ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
617                                ldap_bind($ldap_conn_following_ref, $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'], $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw']);
618                }
619                else
620                        return false;
621
622                if ($recursive == 'true')
623                {
624                        $lists_search = ldap_search($ldap_conn_following_ref, $context, $lists_filter, $justthese);
625                        $users_search = ldap_search($ldap_conn_following_ref, $context, $users_filter, $justthese);
626                }
627                else
628                {
629                        $lists_search = ldap_list($ldap_conn_following_ref, $context, $lists_filter, $justthese);
630                        $users_search = ldap_list($ldap_conn_following_ref, $context, $users_filter, $justthese);
631                }
632               
633                /* email lists */
634                $lists_entries = ldap_get_entries($ldap_conn_following_ref, $lists_search);
635               
636                for ($i=0; $i<$lists_entries["count"]; $i++)
637                {
638                        $l_tmp[$lists_entries[$i]["mail"][0]] = $lists_entries[$i]["cn"][0];
639                }
640                       
641                if (count($l_tmp))
642                        natcasesort($l_tmp);
643                       
644                $i = 0;
645                $lists = array();
646               
647                $options .= '<option  value="-1" disabled>------------------------------&nbsp;&nbsp;&nbsp;&nbsp;'.$this->functions->lang('email lists').'&nbsp;&nbsp;&nbsp;&nbsp;------------------------------ </option>'."\n";       
648                if (count($l_tmp))
649                {
650                        foreach ($l_tmp as $mail => $cn)
651                        {
652                                $options .= "<option value=$mail>$cn</option>";
653                        }
654                        unset($l_tmp);
655                }
656               
657                /* users */
658                $users_entries = ldap_get_entries($ldap_conn_following_ref, $users_search);
659                for ($i=0; $i<$users_entries["count"]; $i++)
660                {
661                        $u_tmp[$users_entries[$i]["mail"][0]] = $users_entries[$i]["cn"][0];
662                }
663                       
664                if (count($u_tmp))
665                        natcasesort($u_tmp);
666                       
667                $i = 0;
668                $users = array();
669               
670                $options .= '<option  value="-1" disabled>-----------------------------&nbsp;&nbsp;&nbsp;&nbsp;'.$this->functions->lang('users').'&nbsp;&nbsp;&nbsp;&nbsp;---------------------------- </option>'."\n";
671                       
672                if (count($u_tmp))
673                {
674                        foreach ($u_tmp as $mail => $cn)
675                        {
676                                $options .= "<option value=$mail class='line-above'>$cn</option>";
677                        }
678                        unset($u_tmp);
679                }
680               
681                ldap_close($ldap_conn_following_ref);
682                return $options;
683        }
684
685        function get_available_groups($params)
686        {
687                $context = $params['context'];
688                $justthese = array("cn", "gidNumber");
689        $groups_list=ldap_list($this->ldap, $context, ("(phpgwAccountType=g)"), $justthese);
690        ldap_sort($this->ldap, $groups_list, "cn");
691       
692        $entries = ldap_get_entries($this->ldap, $groups_list);
693               
694                $options = '';
695                for ($i=0; $i<$entries['count']; $i++)
696                {
697                        $options .= "<option value=" . $entries[$i]['gidnumber'][0] . ">" . $entries[$i]['cn'][0] . "</option>";
698                }
699       
700        return $options;               
701        }
702       
703        function get_available_maillists($params)
704        {
705                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
706                        return false;
707               
708                $context = $params['context'];
709                $justthese = array("uid","mail","uidNumber");
710        $maillists=ldap_list($ldapMasterConnect, $context, ("(phpgwAccountType=l)"), $justthese);
711        ldap_sort($ldapMasterConnect, $maillists, "uid");
712       
713        $entries = ldap_get_entries($ldapMasterConnect, $maillists);
714       
715                $options = '';                 
716                for ($i=0; $i<$entries['count']; $i++)
717                {
718                        $options .= "<option value=" . $entries[$i]['uid'][0] . ">" . $entries[$i]['uid'][0] . " (" . $entries[$i]['mail'][0] . ")" . "</option>";
719                }
720       
721        ldap_close($ldapMasterConnect);
722        return $options;
723        }
724       
725        function ldap_add_entry($dn, $entry)
726        {
727                $result = array();
728                if (!@ldap_add ( $this->ldap, $dn, $entry ))
729                {
730                        $result['status']               = false;
731                        $result['error_number'] = ldap_errno($this->ldap);
732                        $result['msg']                  = $this->functions->lang('Error on function') . " ldap_functions->ldap_add_entry ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_errno($this->ldap) . ldap_error($this->ldap);
733                }
734                else
735                        $result['status'] = true;
736               
737                return $result;
738        }
739       
740        function ldap_save_photo($dn, $pathphoto, $photo_exist=false)
741        {
742                $fd = fopen($pathphoto, "r");
743                $fsize = filesize($pathphoto);
744                $jpegStr = fread($fd, $fsize);
745                fclose ($fd);
746                $attrs['jpegPhoto'] = $jpegStr;
747                       
748                if ($photo_exist)
749                        $res = @ldap_mod_replace($this->ldap, $dn, $attrs);
750                else
751                        $res = @ldap_mod_add($this->ldap, $dn, $attrs);
752                       
753                if ($res)
754                {
755                        $result['status'] = true;
756                }
757                else
758                {
759                        $result['status'] = false;
760                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->ldap_save_photo ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
761                }
762               
763                return $result;
764        }
765       
766        function ldap_remove_photo($dn)
767        {
768                $attrs['jpegPhoto'] = array();
769                $res = ldap_mod_del($this->ldap, $dn, $attrs);
770               
771                if ($res)
772                {
773                        $result['status'] = true;
774                }
775                else
776                {
777                        $result['status'] = false;
778                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->ldap_remove_photo ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
779                }
780               
781                return $result;
782        }       
783       
784        // Pode receber tanto um único memberUid quanto um array de memberUid's
785        function add_user2group($gidNumber, $memberUid)
786        {
787                $filter = "(&(phpgwAccountType=g)(gidNumber=$gidNumber))";
788                $justthese = array("dn");
789                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
790                $entry = ldap_get_entries($this->ldap, $search);
791                $group_dn = $entry[0]['dn'];
792                $attrs['memberUid'] = $memberUid;
793               
794                $res = @ldap_mod_add($this->ldap, $group_dn, $attrs);
795               
796                if ($res)
797                {
798                        $result['status'] = true;
799                }
800                else
801                {
802                        $result['status'] = false;
803                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user2group ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
804                }
805                return $result;
806        }
807       
808        function remove_user2group($gidNumber, $memberUid)
809        {
810                $filter = "(&(phpgwAccountType=g)(gidNumber=$gidNumber))";
811                $justthese = array("dn");
812                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
813                $entry = ldap_get_entries($this->ldap, $search);
814                $group_dn = $entry[0]['dn'];
815                $attrs['memberUid'] = $memberUid;
816                $res = @ldap_mod_del($this->ldap, $group_dn, $attrs);
817               
818                if ($res)
819                {
820                        $result['status'] = true;
821                }
822                else
823                {
824                        $result['status'] = false;
825                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->remove_user2group ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
826                }
827                return $result;
828        }
829       
830        function add_user2maillist($uid, $mail)
831        {
832                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
833                {
834                        $result['status'] = false;
835                        $result['msg'] = $this->functions->lang('Ldap connection fail') . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
836                        return $result;
837                }
838                       
839                $filter = "(&(phpgwAccountType=l)(uid=$uid))";
840                $justthese = array("dn");
841                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
842                $entry = ldap_get_entries($ldapMasterConnect, $search);
843                $group_dn = $entry[0]['dn'];
844                $attrs['mailForwardingAddress'] = $mail;
845                $res = @ldap_mod_add($ldapMasterConnect, $group_dn, $attrs);
846               
847                if ($res)
848                {
849                        $result['status'] = true;
850                }
851                else
852                {
853                        $result['status'] = false;
854                        if (ldap_errno($ldapMasterConnect) == '50')
855                        {
856                                $result['msg'] =        $this->functions->lang('Error on the function') . ' ldap_functions->add_user2maillist' . ".\n" .
857                                                                        $this->functions->lang('The user used for record on LPDA, must have write access') . ".\n";
858                                                                        $this->functions->lang('The user') . ' ' . $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] . ' ' . $this->functions->lang('does not have this access') . ".\n";
859                                                                        $this->functions->lang('Edit Global Catalog Config, in the admin module, and add an user with write access') . ".\n";
860                        }                                       
861                        else
862                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user2maillist ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
863                }
864               
865                ldap_close($ldapMasterConnect);
866                return $result;
867        }
868       
869        function add_user2maillist_scl($dn, $array_emails)
870        {
871                $attrs['mailSenderAddress'] = $array_emails;
872               
873                $res = @ldap_mod_add($this->ldap, $dn, $attrs);
874               
875                if ($res)
876                {
877                        $result['status'] = true;
878                }
879                else
880                {
881                        $result['status'] = false;
882                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user2maillist_scp ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
883                }
884                return $result;
885        }
886
887        function remove_user2maillist($uid, $mail)
888        {
889                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
890                {
891                        $result['status'] = false;
892                        $result['msg'] = $this->functions->lang('Ldap connection fail') . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
893                        return $result;
894                }
895               
896                $filter = "(&(phpgwAccountType=l)(uid=$uid))";
897                $justthese = array("dn");
898                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
899                $entry = ldap_get_entries($ldapMasterConnect, $search);
900                $group_dn = $entry[0]['dn'];
901                $attrs['mailForwardingAddress'] = $mail;
902                $res = @ldap_mod_del($ldapMasterConnect, $group_dn, $attrs);
903               
904                if ($res)
905                {
906                        $result['status'] = true;
907                }
908                else
909                {
910                        $result['status'] = false;
911                        if (ldap_errno($ldapMasterConnect) == '50')
912                        {
913                                $result['msg'] =        $this->functions->lang('Error on the function') . ' ldap_functions->remove_user2maillist' . ".\n" .
914                                                                        $this->functions->lang('The user used for record on LPDA, must have write access') . ".\n";
915                                                                        $this->functions->lang('The user') . ' ' . $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] . ' ' . $this->functions->lang('does not have this access') . ".\n";
916                                                                        $this->functions->lang('Edit Global Catalog Config, in the admin module, and add an user with write access') . ".\n";
917                        }                                       
918                        else
919                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->remove_user2maillist ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
920                }
921                ldap_close($ldapMasterConnect);
922                return $result;
923        }
924
925        function remove_user2maillist_scl($dn, $array_emails)
926        {
927                $attrs['mailSenderAddress'] = $array_emails;
928                $res = @ldap_mod_del($this->ldap, $dn, $attrs);
929               
930                if ($res)
931                {
932                        $result['status'] = true;
933                }
934                else
935                {
936                        $result['status'] = false;
937                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->remove_user2maillist_scp ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
938                }
939                return $result;
940        }
941
942        function replace_user2maillists($new_mail, $old_mail)
943        {
944                $filter = "(&(phpgwAccountType=l)(mailforwardingaddress=$old_mail))";
945                $justthese = array("dn");
946                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
947                $entries = ldap_get_entries($this->ldap, $search);
948                $result['status'] = true;
949                for ($i=0; $i<$entries['count']; $i++)
950                {
951                        $attrs['mailforwardingaddress'] = $old_mail;
952                        $res1 = @ldap_mod_del($this->ldap, $entries[$i]['dn'], $attrs);
953                        $attrs['mailforwardingaddress'] = $new_mail;
954                        $res2 = @ldap_mod_add($this->ldap, $entries[$i]['dn'], $attrs);
955               
956                        if ((!$res1) || (!$res2))
957                        {
958                                $result['status'] = false;
959                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->replace_user2maillists ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
960                        }
961                }
962               
963                return $result;
964        }
965       
966        function get_user_info($uidnumber)
967        {
968                foreach ($this->manager_contexts as $index=>$context)
969                {
970                        $filter="(&(phpgwAccountType=u)(uidNumber=".$uidnumber."))";
971                        $search = ldap_search($this->ldap, $context, $filter);
972                        $entry = ldap_get_entries($this->ldap, $search);
973                       
974                        if ($entry['count'])
975                        {
976                                //Pega o dn do setor do usuario.
977                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
978                                $sector_dn_array = explode(",", $entry[0]['dn']);
979                                for($i=1; $i<count($sector_dn_array); $i++)
980                                        $sector_dn .= $sector_dn_array[$i] . ',';
981                                //Retira ultimo pipe.
982                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
983               
984                                $result['context']                              = $sector_dn;
985                                $result['uid']                                  = $entry[0]['uid'][0];
986                                $result['uidnumber']                    = $entry[0]['uidnumber'][0];
987                                $result['gidnumber']                    = $entry[0]['gidnumber'][0];
988                                $result['departmentnumber']             = $entry[0]['departmentnumber'][0];
989                                $result['givenname']                    = $entry[0]['givenname'][0];
990                                $result['sn']                                   = $entry[0]['sn'][0];
991                                $result['telephonenumber']              = $entry[0]['telephonenumber'][0];
992                                $result['passwd_expired']               = $entry[0]['phpgwlastpasswdchange'][0];
993                                $result['phpgwaccountstatus']   = $entry[0]['phpgwaccountstatus'][0];
994                                $result['phpgwaccountvisible']  = $entry[0]['phpgwaccountvisible'][0];
995                                $result['accountstatus']                = $entry[0]['accountstatus'][0];
996                                $result['mail']                                 = $entry[0]['mail'][0];
997                                $result['mailalternateaddress'] = $entry[0]['mailalternateaddress'];
998                                $result['mailforwardingaddress']= $entry[0]['mailforwardingaddress'];
999                                $result['deliverymode']                 = $entry[0]['deliverymode'][0];
1000                                $result['userPasswordRFC2617']  = $entry[0]['userpasswordrfc2617'][0];
1001
1002                                //Photo
1003                                if ($entry[0]['jpegphoto']['count'] == 1)
1004                                        $result['photo_exist'] = 'true';
1005               
1006                                // Samba
1007                                for ($i=0; $i<$entry[0]['objectclass']['count']; $i++)
1008                                {
1009                                        if ($entry[0]['objectclass'][$i] == 'sambaSamAccount')
1010                                                $result['sambaUser'] = true;
1011                                }
1012                                if (($this->current_config['expressoAdmin_samba_support'] == 'true') && ($result['sambaUser']))
1013                                {
1014                                        $result['sambaaccflags'] = $entry[0]['sambaacctflags'][0];
1015                                        $result['sambalogonscript'] = $entry[0]['sambalogonscript'][0];
1016                                        $result['homedirectory'] = $entry[0]['homedirectory'][0];
1017                                        $a_tmp = explode("-", $entry[0]['sambasid'][0]);
1018                                        array_pop($a_tmp);
1019                                        $result['sambasid'] = implode("-", $a_tmp);
1020                                }
1021
1022                                // Verifica o acesso do gerente aos atributos corporativos
1023                                if ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'manipulate_corporative_information'))
1024                                {
1025                                        $result['corporative_information_employeenumber']= $entry[0]['employeenumber'][0];
1026                                        $result['corporative_information_cpf']                  = $entry[0]['cpf'][0];
1027                                        $result['corporative_information_rg']                   = $entry[0]['rg'][0];
1028                                        $result['corporative_information_rguf']                 = $entry[0]['rguf'][0];
1029                                        $result['corporative_information_description']  = utf8_decode($entry[0]['description'][0]);
1030                                }
1031                               
1032                                // MailLists
1033                                $result['maillists_info'] = $this->get_user_maillists($result['mail']);
1034                                if($result['maillists_info'])
1035                                {
1036                                        foreach ($result['maillists_info'] as $maillist)
1037                                        {
1038                                                $result['maillists'][] = $maillist['uid'];
1039                                        }
1040                                }
1041                               
1042                                // Groups
1043                                $justthese = array("gidnumber","cn");
1044                                $filter="(&(phpgwAccountType=g)(memberuid=".$result['uid']."))";
1045                                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1046                        ldap_sort($this->ldap, $search, "cn");
1047                        $entries = ldap_get_entries($this->ldap, $search);
1048                        for ($i=0; $i<$entries['count']; $i++)
1049                        {
1050                                $result['groups_ldap'][ $entries[$i]['gidnumber'][0] ] = $entries[$i]['cn'][0];
1051                        }
1052                        }
1053                }
1054                if (is_array($result))
1055                        return $result;
1056                else
1057                        return false;
1058        }
1059               
1060        function get_user_maillists($mail)
1061        {
1062                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
1063                        return false;
1064               
1065                $result = array();
1066               
1067                //Mostra somente os mailists dos contextos do gerente
1068                $justthese = array("uid","mail","uidnumber");
1069                $filter="(&(phpgwAccountType=l)(mailforwardingaddress=$mail))";
1070               
1071                foreach ($this->manager_contexts as $index=>$context)
1072                {
1073                        $search = ldap_search($ldapMasterConnect, $context, $filter, $justthese);
1074                $entries = ldap_get_entries($ldapMasterConnect, $search);
1075               
1076                for ($i=0; $i<$entries['count']; $i++)
1077                {
1078                                $result[ $entries[$i]['uid'][0] ]['uid']                = $entries[$i]['uid'][0];
1079                                $result[ $entries[$i]['uid'][0] ]['mail']               = $entries[$i]['mail'][0];
1080                               
1081                                $a_tmp[] = $entries[$i]['uid'][0];
1082                }
1083                }
1084       
1085        if($a_tmp) {
1086                natcasesort($a_tmp);
1087       
1088                foreach ($a_tmp as $uid)
1089                {
1090                                $return[$uid]['uid']            = $result[$uid]['uid'];
1091                                $return[$uid]['mail']           = $result[$uid]['mail'];
1092                }
1093        }
1094        ldap_close($ldapMasterConnect);
1095                return $return;
1096        }
1097       
1098        function get_group_info($gidnumber)
1099        {
1100                foreach ($this->manager_contexts as $index=>$context)
1101                {
1102                        $filter="(&(phpgwAccountType=g)(gidNumber=".$gidnumber."))";
1103                        $search = ldap_search($this->ldap, $context, $filter);
1104                        $entry = ldap_get_entries($this->ldap, $search);
1105                       
1106                        if ($entry['count'])
1107                        {
1108                                //Pega o dn do setor do grupo.
1109                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
1110                                $sector_dn_array = explode(",", $entry[0]['dn']);
1111                                for($i=1; $i<count($sector_dn_array); $i++)
1112                                        $sector_dn .= $sector_dn_array[$i] . ',';
1113                                //Retira ultimo pipe.
1114                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
1115               
1116                                $result['context']                              = $sector_dn;
1117                                $result['cn']                                   = $entry[0]['cn'][0];
1118                                $result['description']                  = $entry[0]['description'][0];
1119                                $result['gidnumber']                    = $entry[0]['gidnumber'][0];
1120                                $result['phpgwaccountvisible']  = $entry[0]['phpgwaccountvisible'][0];
1121                                $result['email']                                = $entry[0]['mail'][0];
1122               
1123                                //MemberUid
1124                                for ($i=0; $i<$entry[0]['memberuid']['count']; $i++)
1125                                {
1126                                        $justthese = array("cn","uid","uidnumber");
1127                       
1128                                        // Montagem dinamica do filtro
1129                                        $filter="(&(phpgwAccountType=u)(|";
1130                                        for ($k=0; (($k<10) && ($i<$entry[0]['memberuid']['count'])); $k++)
1131                                        {
1132                                                $filter .= "(uid=".$entry[0]['memberuid'][$i].")";
1133                                                $i++;
1134                                        }
1135                                        $i--;
1136                                        $filter .= "))";
1137                       
1138                                        $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1139                                        $user_entry = ldap_get_entries($this->ldap, $search);
1140
1141                                        for ($j=0; $j<$user_entry['count']; $j++)
1142                                        {
1143                                                $result['memberuid_info'][$user_entry[$j]['uid'][0]]['cn'] = $user_entry[$j]['cn'][0];
1144                                                $result['memberuid_info'][$user_entry[$j]['uid'][0]]['uidnumber'] = $user_entry[$j]['uidnumber'][0];
1145                                                $result['memberuid_info'][$user_entry[$j]['uid'][0]]['type'] = 'u';
1146                                        }
1147                                }
1148               
1149                                // Checamos e-mails que não fazem parte do expresso.
1150                                // Criamos um array temporario
1151                                $tmp_array = array();
1152                                if($result['memberuid_info'])
1153                                        foreach ($result['memberuid_info'] as $uid => $user_data)
1154                                        {
1155                                                $tmp_array[] = $uid;
1156                                        }
1157               
1158                                if($entry[0]['memberuid']) {
1159                                        // Retira o count do array
1160                                        array_shift($entry[0]['memberuid']);
1161                                        // Vemos a diferença
1162                                        $array_diff = array_diff($entry[0]['memberuid'], $tmp_array);
1163                                        // Incluimos no resultado                       
1164                                        foreach ($array_diff as $index=>$uid)
1165                                        {
1166                                                $result['memberuid_info'][$uid]['cn'] = $uid;
1167                                        }
1168                                }
1169               
1170                                // Samba
1171                                for ($i=0; $i<$entry[0]['objectclass']['count']; $i++)
1172                                {
1173                                        if ($entry[0]['objectclass'][$i] == 'sambaGroupMapping')
1174                                                $result['sambaGroup'] = true;
1175
1176                                        $a_tmp = explode("-", $entry[0]['sambasid'][0]);
1177                                        array_pop($a_tmp);
1178                                        $result['sambasid'] = implode("-", $a_tmp);
1179                                }
1180                                return $result;
1181                        }
1182                }
1183        }       
1184       
1185        function get_maillist_info($uidnumber)
1186        {
1187                /* folling referral connection */
1188                $ldap_conn_following_ref = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host']);
1189                if ($ldap_conn_following_ref)
1190                {
1191                        ldap_set_option($ldap_conn_following_ref, LDAP_OPT_PROTOCOL_VERSION, 3);
1192                        ldap_set_option($ldap_conn_following_ref, LDAP_OPT_REFERRALS, 1);
1193
1194                        if ( ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
1195                                ldap_bind($ldap_conn_following_ref, $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'], $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw']);
1196                }
1197               
1198                foreach ($this->manager_contexts as $index=>$context)
1199                {
1200                        $filter="(&(phpgwAccountType=l)(uidNumber=".$uidnumber."))";
1201                        $search = ldap_search($this->ldap, $context, $filter);
1202                        $entry = ldap_get_entries($this->ldap, $search);
1203                       
1204                        if ($entry['count'])
1205                        {
1206                                //Pega o dn do setor do usuario.
1207                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
1208                                $sector_dn_array = explode(",", $entry[0]['dn']);
1209                                for($i=1; $i<count($sector_dn_array); $i++)
1210                                        $sector_dn .= $sector_dn_array[$i] . ',';
1211                                //Retira ultimo pipe.
1212                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
1213                       
1214                                $result['context']                              = $sector_dn;
1215                                $result['uidnumber']                    = $entry[0]['uidnumber'][0];
1216                                $result['uid']                                  = strtolower($entry[0]['uid'][0]);
1217                                $result['cn']                                   = $entry[0]['cn'][0];
1218                                $result['mail']                                 = $entry[0]['mail'][0];
1219                                $result['description']                  = utf8_decode($entry[0]['description'][0]);
1220                                $result['accountStatus']                = $entry[0]['accountstatus'][0];
1221                                $result['phpgwAccountVisible']  = $entry[0]['phpgwaccountvisible'][0];
1222                       
1223                                //Members
1224                                for ($i=0; $i<$entry[0]['mailforwardingaddress']['count']; $i++)
1225                                {
1226                                        $justthese = array("cn", "uidnumber", "uid", "phpgwaccounttype", "mail");
1227                               
1228                                        // Montagem dinamica do filtro, para nao ter muitas conexoes com o ldap
1229                                        $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|";
1230                                        for ($k=0; (($k<10) && ($i<$entry[0]['mailforwardingaddress']['count'])); $k++)
1231                                        {
1232                                                $filter .= "(mail=".$entry[0]['mailforwardingaddress'][$i].")";
1233                                                $i++;
1234                                        }
1235                                        $i--;
1236                                        $filter .= "))";
1237                               
1238                                        $search = ldap_search($ldap_conn_following_ref, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1239                                        $user_entry = ldap_get_entries($ldap_conn_following_ref, $search);
1240                                                                       
1241                                        for ($j=0; $j<$user_entry['count']; $j++)
1242                                        {
1243                                                $result['mailForwardingAddress_info'][$user_entry[$j]['mail'][0]]['uid'] = $user_entry[$j]['uid'][0];
1244                                                $result['mailForwardingAddress_info'][$user_entry[$j]['mail'][0]]['cn'] = $user_entry[$j]['cn'][0];
1245                                                $result['mailForwardingAddress_info'][$user_entry[$j]['mail'][0]]['type'] = $user_entry[$j]['phpgwaccounttype'][0];
1246                                                $result['mailForwardingAddress'][] = $user_entry[$j]['mail'][0];
1247                                        }
1248                                }
1249
1250                                // Emails não encontrados no ldap
1251                                array_shift($entry[0]['mailforwardingaddress']); //Retira o count do array
1252                                $missing_emails = array_diff($entry[0]['mailforwardingaddress'], $result['mailForwardingAddress']);
1253                               
1254                                // Incluimos estes no resultado
1255                                foreach ($missing_emails as $index=>$mailforwardingaddress)
1256                                {
1257                                        $result['mailForwardingAddress_info'][$mailforwardingaddress]['uid'] = $mailforwardingaddress;
1258                                        $result['mailForwardingAddress_info'][$mailforwardingaddress]['cn'] = 'E-Mail nao encontrado';
1259                                        $result['mailForwardingAddress'][] = $mailforwardingaddress;
1260                                }
1261                               
1262                                ldap_close($ldap_conn_following_ref);
1263                                return $result;
1264                        }
1265                }
1266        }       
1267
1268        function get_maillist_scl_info($uidnumber)
1269        {
1270                foreach ($this->manager_contexts as $index=>$context)
1271                {
1272                        $filter="(&(phpgwAccountType=l)(uidNumber=$uidnumber))";
1273                        $search = ldap_search($this->ldap, $context, $filter);
1274                        $entry = ldap_get_entries($this->ldap, $search);
1275
1276                        if ($entry['count'])
1277                        {
1278                                //Pega o dn do setor do usuario.
1279                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
1280                                $sector_dn_array = explode(",", $entry[0]['dn']);
1281                                for($i=1; $i<count($sector_dn_array); $i++)
1282                                        $sector_dn .= $sector_dn_array[$i] . ',';
1283                                //Retira ultimo pipe.
1284                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
1285               
1286                                $result['dn']                                           = $entry[0]['dn'];
1287                                $result['context']                                      = $sector_dn;
1288                                $result['uidnumber']                            = $entry[0]['uidnumber'][0];
1289                                $result['uid']                                          = $entry[0]['uid'][0];
1290                                $result['cn']                                           = $entry[0]['cn'][0];
1291                                $result['mail']                                         = $entry[0]['mail'][0];
1292                                $result['accountStatus']                        = $entry[0]['accountstatus'][0];
1293                                $result['phpgwAccountVisible']          = $entry[0]['phpgwaccountvisible'][0];
1294                                $result['accountRestrictive']           = $entry[0]['accountrestrictive'][0];
1295                                $result['participantCanSendMail']       = $entry[0]['participantcansendmail'][0];
1296               
1297                                //Senders
1298                                for ($i=0; $i<$entry[0]['mailsenderaddress']['count']; $i++)
1299                                {
1300                                        $justthese = array("cn", "uidnumber", "uid", "mail");
1301                                        $filter="(&(phpgwAccountType=u)(mail=".$entry[0]['mailsenderaddress'][$i]."))";
1302                                        $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1303                                        $user_entry = ldap_get_entries($this->ldap, $search);
1304                       
1305                                        $result['senders_info'][$user_entry[0]['mail'][0]]['uid'] = $user_entry[0]['uid'][0];
1306                                        $result['senders_info'][$user_entry[0]['mail'][0]]['cn'] = $user_entry[0]['cn'][0];
1307                                        $result['members'][] = $user_entry[0]['mail'][0];
1308                                }
1309                                return $result;
1310                        }
1311                }
1312        }       
1313
1314        function group_exist($gidnumber)
1315        {
1316                $justthese = array("cn");
1317                $filter="(&(phpgwAccountType=g)(gidNumber=".$gidnumber."))";
1318                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1319                               
1320                $entry = ldap_get_entries($this->ldap, $search);
1321                if ($entry['count'] == 0)
1322                        return false;
1323                else
1324                        return true;
1325        }
1326
1327        function gidnumbers2cn($gidnumbers)
1328        {
1329                $result = array();
1330                if (count($gidnumbers))
1331                {
1332                        $justthese = array("cn");
1333                        $i = 0;
1334                        foreach ($gidnumbers as $gidnumber)
1335                        {
1336                                $filter="(&(phpgwAccountType=g)(gidNumber=".$gidnumber."))";
1337                                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1338                               
1339                                $entry = ldap_get_entries($this->ldap, $search);
1340                                if ($entry['count'] == 0)
1341                                        $result['groups_info'][$i]['cn'] = '_' . $this->functions->lang('group only exist on DB, but does not exist on ldap');
1342                                       
1343                                else
1344                                        $result['groups_info'][$i]['cn'] = $entry[0]['cn'][0];
1345                                $result['groups_info'][$i]['gidnumber'] = $gidnumber;
1346                       
1347                                /* o gerente pode excluir um grupo de um usuario onde este grupo esta em outra OU ? */
1348                                /* é o mesmo que o manager editar um grupo de outra OU */
1349                                $result['groups_info'][$i]['group_disabled'] = 'true';
1350                                foreach ($this->manager_contexts as $index=>$context)
1351                                {
1352                                        if (strpos(strtolower($entry[0]['dn']), strtolower($context)))
1353                                        {
1354                                                $result['groups_info'][$i]['group_disabled'] = 'false';
1355                                        }
1356                                }
1357
1358                                $i++;
1359                        }
1360                }
1361                return $result;
1362        }
1363
1364        function uidnumber2uid($uidnumber)
1365        {
1366                $justthese = array("uid");
1367                $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uidNumber=".$uidnumber."))";
1368                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1369                $entry = ldap_get_entries($this->ldap, $search);
1370                return $entry[0]['uid'][0];
1371        }
1372
1373        function uidnumber2mail($uidnumber)
1374        {
1375                $justthese = array("mail");
1376                $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uidNumber=".$uidnumber."))";
1377                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1378                $entry = ldap_get_entries($this->ldap, $search);
1379                return $entry[0]['mail'][0];
1380        }
1381
1382       
1383        function change_user_context($dn, $newrdn, $newparent)
1384        {
1385                if (!ldap_rename ( $this->ldap, $dn, $newrdn, $newparent, true ))
1386                {
1387                        $return['status'] = false;
1388                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->change_user_context ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1389                }
1390                else
1391                        $return['status'] = true;
1392               
1393                return $return;
1394        }
1395       
1396        function replace_user_attributes($dn, $ldap_mod_replace)
1397        {
1398                if (!@ldap_mod_replace ( $this->ldap, $dn, $ldap_mod_replace ))
1399                {
1400                        $return['status'] = false;
1401                        $return['error_number'] = ldap_errno($this->ldap);
1402                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->replace_user_attributes ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1403                }
1404                else
1405                        $return['status'] = true;
1406               
1407                return $return;
1408        }
1409       
1410        function add_user_attributes($dn, $ldap_add)
1411        {
1412                if (!@ldap_mod_add ( $this->ldap, $dn, $ldap_add ))
1413                {
1414                        $return['status'] = false;
1415                        $return['error_number'] = ldap_errno($this->ldap);
1416                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user_attributes ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1417                }
1418                else
1419                        $return['status'] = true;
1420               
1421                return $return;
1422        }
1423       
1424        function remove_user_attributes($dn, $ldap_remove)
1425        {
1426                if (!@ldap_mod_del ( $this->ldap, $dn, $ldap_remove ))
1427                {
1428                        $return['status'] = false;
1429                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->remove_user_attributes ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1430                }
1431                else
1432                        $return['status'] = true;
1433               
1434                return $return;
1435        }
1436       
1437        function set_user_password($uid, $password)
1438        {
1439                $justthese = array("userPassword");
1440                $filter="(&(phpgwAccountType=u)(uid=".$uid."))";
1441                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1442            $entry = ldap_get_entries($this->ldap, $search);
1443                $dn = $entry[0]['dn'];
1444                $userPassword = $entry[0]['userpassword'][0];
1445                $ldap_mod_replace['userPassword'] = $password;
1446                $this->replace_user_attributes($dn, $ldap_mod_replace);
1447                return $userPassword;
1448        }
1449       
1450        function delete_user($user_info)
1451        {
1452                // Verifica acesso do gerente (OU) ao tentar deletar um usuário.
1453                $manager_access = false;
1454                foreach ($this->manager_contexts as $index=>$context)
1455                {
1456                        if ( (strpos(strtolower($user_info['context']), strtolower($context))) || (strtolower($user_info['context']) == strtolower($context)) )
1457                        {
1458                                $manager_access = true;
1459                                break;
1460                        }
1461                }
1462                if (!$manager_access)
1463                {
1464                        $return['status'] = false;
1465                        $result['msg'] = $this->functions->lang('You do not have access to delete this user') . ".";
1466                        return $return;
1467                }
1468               
1469                $return['status'] = true;
1470                $return['msg'] = "";
1471                               
1472                // GROUPS
1473                $attrs = array();
1474                $attrs['memberuid'] = $user_info['uid'];
1475               
1476                if (count($user_info['groups_info']))
1477                {
1478                        foreach ($user_info['groups_info'] as $group_info)
1479                        {
1480                                $gidnumber = $group_info['gidnumber'];
1481                                $justthese = array("dn");
1482                                $filter="(&(phpgwAccountType=g)(gidnumber=".$gidnumber."))";
1483                                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1484                        $entry = ldap_get_entries($this->ldap, $search);
1485                                $dn = $entry[0]['dn'];
1486
1487                                if (!@ldap_mod_del($this->ldap, $dn, $attrs))
1488                                {
1489                                        $return['status'] = false;
1490                                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_user from group ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1491                                }
1492                        }
1493                }
1494               
1495                //INSTITUTIONAL ACCOUNTS
1496                $attrs = array();
1497                $attrs['mailForwardingAddress'] = $user_info['mail'];
1498               
1499                $justthese = array("dn");
1500                $filter="(&(phpgwAccountType=i)(mailforwardingaddress=".$user_info['mail']."))";
1501                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1502            $entries = ldap_get_entries($this->ldap, $search);
1503               
1504                for ($i=0; $i<$entries['count']; $i++)
1505                {
1506                        if ( !@ldap_mod_del($this->ldap, $entries[$i]['dn'], $attrs) )
1507                        {
1508                                $result['status'] = false;
1509                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_user, institutional accounts ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1510                        }
1511                }
1512               
1513                // MAILLISTS
1514                $attrs = array();
1515                $attrs['mailForwardingAddress'] = $user_info['mail'];
1516               
1517                if (count($user_info['maillists_info']))
1518                {
1519                       
1520                        if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
1521                        {
1522                                $return['status'] = false;
1523                                $result['msg'] = $this->functions->lang('Connection with ldap_master fail') . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1524                                return $return;
1525                        }
1526                       
1527                        foreach ($user_info['maillists_info'] as $maillists_info)
1528                        {
1529                                $uid = $maillists_info['uid'];
1530                                $justthese = array("dn");
1531                                $filter="(&(phpgwAccountType=l)(uid=".$uid."))";
1532                                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1533                        $entry = ldap_get_entries($ldapMasterConnect, $search);
1534                                $dn = $entry[0]['dn'];
1535                       
1536                                if (!@ldap_mod_del($ldapMasterConnect, $dn, $attrs))
1537                                {
1538                                        $return['status'] = false;
1539                                        if (ldap_errno($ldapMasterConnect) == '50')
1540                                        {
1541                                                $result['msg'] =        $this->functions->lang('Error on the function') . ' ldap_functions->add_user2maillist' . ".\n" .
1542                                                                                        $this->functions->lang('The user used for record on LPDA, must have write access') . ".\n";
1543                                                                                        $this->functions->lang('The user') . ' ' . $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] . ' ' . $this->functions->lang('does not have this access') . ".\n";
1544                                                                                        $this->functions->lang('Edit Global Catalog Config, in the admin module, and add an user with write access') . ".\n";
1545                                        }
1546                                        else
1547                                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_user, email lists ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
1548                                }
1549                        }
1550                        ldap_close($ldapMasterConnect);
1551                }
1552                       
1553                // UID
1554                $dn = "uid=" . $user_info['uid'] . "," . $user_info['context'];
1555                if (!@ldap_delete($this->ldap, $dn))
1556                {
1557                        $return['status'] = false;
1558                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_user, email lists ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
1559                }
1560                /* jakjr */
1561                return $return;
1562        }
1563       
1564        function delete_maillist($uidnumber, $mail)
1565        {
1566                $return['status'] = true;
1567               
1568                $justthese = array("dn");
1569               
1570                // remove listas dentro de listas
1571                $filter="(&(phpgwAccountType=l)(mailForwardingAddress=".$mail."))";
1572                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1573                $entry = ldap_get_entries($this->ldap, $search);
1574                $attrs['mailForwardingAddress'] = $mail;
1575                for ($i=0; $i<=$entry['count']; $i++)
1576            {
1577                        $dn = $entry[$i]['dn'];
1578                @ldap_mod_del ( $this->ldap, $dn,  $attrs);
1579            }
1580               
1581                $filter="(&(phpgwAccountType=l)(uidnumber=".$uidnumber."))";
1582                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1583                $entry = ldap_get_entries($this->ldap, $search);
1584                $dn = $entry[0]['dn'];
1585               
1586                if (!@ldap_delete($this->ldap, $dn))
1587                {
1588                        $return['status'] = false;
1589                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_maillist ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1590                }
1591               
1592                return $return;
1593        }
1594
1595        function delete_group($gidnumber)
1596        {
1597                $return['status'] = true;
1598               
1599                $justthese = array("dn");
1600                $filter="(&(phpgwAccountType=g)(gidnumber=".$gidnumber."))";
1601                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1602                $entry = ldap_get_entries($this->ldap, $search);
1603                $dn = $entry[0]['dn'];
1604               
1605                if (!@ldap_delete($this->ldap, $dn))
1606                {
1607                        $return['status'] = false;
1608                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_group ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1609                }
1610               
1611                return $return;
1612        }
1613
1614        function check_access_to_renamed($uid)
1615        {
1616                $justthese = array("dn");
1617                $filter="(&(phpgwAccountType=u)(uid=$uid))";
1618               
1619                foreach ($this->manager_contexts as $index=>$context)
1620                {
1621                        $search = ldap_search($this->ldap, $context, $filter, $justthese);
1622                        $entry = ldap_get_entries($this->ldap, $search);
1623                        if ($entry['count'])
1624                                return true;
1625                }
1626            return false;
1627        }
1628
1629        function check_rename_new_uid($uid)
1630        {
1631                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
1632                        return false;
1633               
1634                $justthese = array("dn");
1635                $filter="(&(phpgwAccountType=u)(uid=$uid))";
1636               
1637                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1638                $count_entries = @ldap_count_entries($ldapMasterConnect, $search);
1639               
1640                if ($count_entries)
1641                        return false;
1642                       
1643                return true;
1644        }
1645       
1646        function rename_uid($uid, $new_uid)
1647        {
1648                $return['status'] = true;
1649               
1650                $justthese = array("dn");
1651                $filter="(&(phpgwAccountType=u)(uid=".$uid."))";
1652                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1653            $entry = ldap_get_entries($this->ldap, $search);
1654                $dn = $entry[0]['dn'];
1655               
1656                $explode_dn = ldap_explode_dn($dn, 0);
1657                $rdn = "uid=" . $new_uid;
1658
1659                $parent = array();
1660                for ($j=1; $j<(count($explode_dn)-1); $j++)
1661                        $parent[] = $explode_dn[$j];
1662                $parent = implode(",", $parent);
1663               
1664                $return['new_dn'] = $rdn . ',' . $parent;
1665                       
1666                if (!@ldap_rename($this->ldap, $dn, $rdn, $parent, true))
1667                {
1668                        $return['status'] = false;
1669                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->rename_uid ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1670                }
1671               
1672                //Grupos
1673                $justthese = array("dn");
1674                $filter="(&(phpgwAccountType=g)(memberuid=".$uid."))";
1675                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1676            $entry = ldap_get_entries($this->ldap, $search);
1677        $array_mod_add['memberUid'] = $new_uid;
1678        $array_mod_del['memberUid'] = $uid;
1679
1680            for ($i=0; $i<=$entry['count']; $i++)
1681            {
1682                $dn = $entry[$i]['dn'];
1683                @ldap_mod_add ( $this->ldap, $dn,  $array_mod_add);
1684                @ldap_mod_del ( $this->ldap, $dn,  $array_mod_del);
1685            }
1686                return $return;
1687        }
1688
1689        function rename_cn($cn, $new_cn)
1690        {
1691                $return['status'] = true;
1692               
1693                $justthese = array("dn");
1694                $filter="(&(phpgwAccountType=g)(uid=".$cn."))";
1695                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1696            $entry = ldap_get_entries($this->ldap, $search);
1697                $dn = $entry[0]['dn'];
1698               
1699                $explode_dn = ldap_explode_dn($dn, 0);
1700                $rdn = "cn=" . $new_cn;
1701
1702                $parent = array();
1703                for ($j=1; $j<(count($explode_dn)-1); $j++)
1704                        $parent[] = $explode_dn[$j];
1705                $parent = implode(",", $parent);
1706               
1707                $return['new_dn'] = $rdn . ',' . $parent;
1708                       
1709                if (!@ldap_rename($this->ldap, $dn, $rdn, $parent, false))
1710                {
1711                        $return['status'] = false;
1712                }
1713               
1714                return $return;
1715        }
1716       
1717        function exist_sambadomains($contexts, $sambaDomainName)
1718        {
1719                $justthese = array("dn");
1720                $filter="(&(objectClass=sambaDomain)(sambaDomainName=$sambaDomainName))";
1721               
1722                foreach ($contexts as $index=>$context)
1723                {
1724                        $search = ldap_search($this->ldap, $context, $filter, $justthese);
1725                    $entry = ldap_get_entries($this->ldap, $search);
1726           
1727                        if ($entry['count'])
1728                                return true;
1729                }
1730                return false;
1731        }
1732       
1733        // Primeiro nilvel de organização.
1734        function exist_sambadomains_in_context($params)
1735        {
1736                $dn = $GLOBALS['phpgw_info']['server']['ldap_context'];
1737                $array_dn = ldap_explode_dn ( $dn, 0 );
1738               
1739                $context = $params['context'];
1740                $array_context = ldap_explode_dn ( $context, 0 );
1741               
1742                // Pego o setor no caso do contexto ser um sub-setor.
1743                if (($array_dn['count']+1) < ($array_context['count']))
1744                {
1745                        // inverto o array_dn para poder retirar o count
1746                        $array_dn_reverse  = array_reverse ( $array_dn, false );
1747                       
1748                        //retiro o count
1749                        array_pop($array_dn_reverse);
1750                       
1751                        //incluo o setor no dn
1752                        array_push ( $array_dn_reverse,  $array_context[ $array_context['count'] - 1 - $array_dn['count']]);
1753                       
1754                        // Volto a ordem natural
1755                        $array_dn  = array_reverse ( $array_dn_reverse, false );
1756                       
1757                        // Implodo
1758                        $context = implode ( ",", $array_dn );
1759                }
1760               
1761                $justthese = array("dn","sambaDomainName");
1762                $filter="(objectClass=sambaDomain)";
1763                $search = ldap_list($this->ldap, $context, $filter, $justthese);
1764            $entry = ldap_get_entries($this->ldap, $search);
1765           
1766            for ($i=0; $i<$entry['count']; $i++)
1767            {
1768                        $return['sambaDomains'][$i] = $entry[$i]['sambadomainname'][0];
1769            }
1770           
1771                if ($entry['count'])
1772                        $return['status'] = true;
1773                else
1774                        $return['status'] = false;
1775                       
1776                return $return;
1777        }
1778        function exist_domain_name_sid($sambadomainname, $sambasid)
1779        {
1780                $context = $GLOBALS['phpgw_info']['server']['ldap_context'];
1781
1782                $justthese = array("dn","sambaDomainName");
1783                $filter="(&(objectClass=sambaDomain)(sambaSID=$sambasid)(sambaDomainName=$sambadomainname))";
1784                $search = ldap_search($this->ldap, $context, $filter, $justthese);
1785            $count_entries = ldap_count_entries($this->ldap, $search);
1786           
1787            if ($count_entries > 0)
1788                return true;
1789            else
1790                return false;
1791        }
1792       
1793        function add_sambadomain($sambadomainname, $sambasid, $context)
1794        {
1795                $result = array();
1796               
1797                $dn                                                             = "sambaDomainName=$sambadomainname,$context";
1798                $entry['sambaSID']                                      = $sambasid;
1799                $entry['objectClass']                           = 'sambaDomain';
1800                $entry['sambaAlgorithmicRidBase']       = '1000';
1801                $entry['sambaDomainName']                       = $sambadomainname;
1802               
1803                if (!@ldap_add ( $this->ldap, $dn, $entry ))
1804                {
1805                        $return['status'] = false;
1806                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_sambadomain ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1807                }
1808                else
1809                        $return['status'] = true;
1810               
1811                return $return;
1812        }
1813       
1814        function delete_sambadomain($sambadomainname)
1815        {
1816                $return['status'] = true;
1817                $filter="(sambaDomainName=$sambadomainname)";
1818                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter);
1819            $entry = ldap_get_entries($this->ldap, $search);
1820         
1821                if ($entry['count'] != 0)
1822            {
1823                        $dn = $entry[0]['dn'];
1824                       
1825                        if (!@ldap_delete($this->ldap, $dn))
1826                        {
1827                                $return['status'] = false;
1828                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_sambadomain ($sambadomainname)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1829                        }
1830            }
1831           
1832                return $return;
1833        }
1834       
1835        function search_user($params)
1836        {
1837                $search = $params['search'];
1838                $justthese = array("cn","uid", "mail");
1839        $users_list=ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], "(&(phpgwAccountType=u) (|(cn=*$search*)(mail=$search*)) )", $justthese);
1840       
1841        if (ldap_count_entries($this->ldap, $users_list) == 0)
1842        {
1843                $return['status'] = 'false';
1844                $result['msg'] = $this->functions->lang('Any result was found') . '.';
1845                return $return;
1846        }
1847       
1848        ldap_sort($this->ldap, $users_list, "cn");
1849       
1850        $entries = ldap_get_entries($this->ldap, $users_list);
1851               
1852                $options = '';
1853                for ($i=0; $i<$entries['count']; $i++)
1854                {
1855                        $options .= "<option value=" . $entries[$i]['uid'][0] . ">" . $entries[$i]['cn'][0] . " (".$entries[$i]['mail'][0].")" . "</option>";
1856                }
1857       
1858        return $options;               
1859        }
1860       
1861        function create_institutional_accounts($params)
1862        {
1863                /* Begin: Access verification */
1864                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'add_institutional_accounts'))
1865                {
1866                        $return['status'] = false;
1867                        $return['msg'] = $this->functions->lang('You do not have right to create institutional accounts') . ".";
1868                        return $return;
1869                }
1870               
1871                $access_granted = false;
1872                foreach ($this->manager_contexts as $idx=>$manager_context)
1873                {
1874                        if (stristr($params['context'], $manager_context))
1875                        {
1876                                $access_granted = true;
1877                                break;
1878                        }
1879                }
1880                if (!$access_granted)
1881                {
1882                        $return['status'] = false;
1883                        $return['msg'] = $this->functions->lang('You do not have access to this organization') . ".";
1884                        return $return;
1885                }
1886                /* End: Access verification */
1887
1888                /* Begin: Validation */
1889                if ( (empty($params['cn'])) || (empty($params['mail'])) )
1890                {
1891                        $result['status'] = false;
1892                        $result['msg']  = $this->functions->lang('Field mail or name is empty');
1893                        return $result;
1894                }
1895
1896                if (! eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$", $params['mail']) )
1897                {
1898                        $result['status'] = false;
1899                        $result['msg']  = $this->functions->lang('Field mail is not formed correcty') . '.';
1900                        return $result;
1901                }
1902
1903                $uid = 'institutional_account_' . $params['mail'];
1904                $dn = "uid=$uid," . $params['context'];
1905
1906                $filter = "(mail=".$params['mail'].")";
1907                $justthese = array("cn");
1908                $search = @ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1909                $entries = @ldap_get_entries($this->ldap,$search);
1910                if ($entries['count'] != 0)
1911                {
1912                        $result['status'] = false;
1913                        $result['msg'] = $this->functions->lang('Field mail already in use');
1914                        return $result;
1915                }
1916                /* End: Validation */
1917                                               
1918                $info = array();
1919                $info['cn']                                     = $params['cn'];
1920                $info['sn']                                     = $params['cn'];
1921                $info['uid']                            = $uid;
1922                $info['mail']                           = $params['mail'];
1923                $info['description']            = $params['description'];
1924                $info['phpgwAccountType']       = 'i';
1925                $info['objectClass'][]          = 'inetOrgPerson';
1926                $info['objectClass'][]          = 'phpgwAccount';
1927                $info['objectClass'][]          = 'top';
1928                $info['objectClass'][]          = 'person';
1929                $info['objectClass'][]          = 'qmailUser';
1930                $info['objectClass'][]          = 'organizationalPerson';
1931               
1932                if ($params['accountStatus'] == 'on')
1933                {
1934                        $info['accountStatus'] = 'active';
1935                }
1936                if ($params['phpgwAccountVisible'] == 'on')
1937                {
1938                        $info['phpgwAccountVisible'] = '-1';
1939                }
1940               
1941                if (!empty($params['owners']))
1942                {
1943                        foreach($params['owners'] as $index=>$uidnumber)
1944                        {
1945                                $info['mailForwardingAddress'][] = $this->uidnumber2mail($uidnumber);
1946                        }
1947                }               
1948               
1949                $result = array();
1950                if (!@ldap_add ( $this->ldap, $dn, $info ))
1951                {
1952                        $result['status'] = false;
1953                        $result['msg']  = $this->functions->lang('Error on function') . ' ldap_functions->create_institutional_accounts';
1954                        $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
1955                }
1956                else
1957                        $result['status'] = true;
1958               
1959                return $result;
1960        }
1961       
1962        function save_institutional_accounts($params)
1963        {
1964                /* Begin: Access verification */
1965                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'edit_institutional_accounts'))
1966                {
1967                        $return['status'] = false;
1968                        $return['msg'] = $this->functions->lang('You do not have right to edit institutional accounts') . ".";
1969                        return $return;
1970                }
1971                $access_granted = false;
1972                foreach ($this->manager_contexts as $idx=>$manager_context)
1973                {
1974                        if (stristr($params['context'], $manager_context))
1975                        {
1976                                $access_granted = true;
1977                                break;
1978                        }
1979                }
1980                if (!$access_granted)
1981                {
1982                        $return['status'] = false;
1983                        $return['msg'] = $this->functions->lang('You do not have access to this organization') . ".";
1984                        return $return;
1985                }
1986                /* End: Access verification */
1987               
1988                /* Begin: Validation */
1989                if ( (empty($params['cn'])) || (empty($params['mail'])) )
1990                {
1991                        $result['status'] = false;
1992                        $result['msg']  = $this->functions->lang('Field mail or name is empty') . '.';
1993                        return $result;
1994                }
1995
1996                if (! eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$", $params['mail']) )
1997                {
1998                        $result['status'] = false;
1999                        $result['msg']  = $this->functions->lang('Field mail is not formed correcty') . '.';
2000                        return $result;
2001                }
2002
2003                $uid = 'institutional_account_' . $params['mail'];
2004                $dn = strtolower("uid=$uid," . $params['context']);
2005                $anchor = strtolower($params['anchor']);
2006
2007                $filter = "(mail=".$params['mail'].")";
2008                $justthese = array("cn");
2009                $search = @ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
2010                $entries = @ldap_get_entries($this->ldap,$search);
2011               
2012                if ( ($entries['count'] > 1) || (($entries['count'] == 1) && ($entries[0]['dn'] != $anchor)) )
2013                {
2014                        $result['status'] = false;
2015                        $result['msg'] = $this->functions->lang('Field mail already in use.');
2016                        return $result;
2017                }
2018                /* End: Validation */
2019               
2020                $result = array();
2021                $result['status'] = true;
2022               
2023                if ($anchor != $dn)
2024                {
2025                        if (!@ldap_rename($this->ldap, $anchor, "uid=$uid", $params['context'], true))
2026                        {
2027                                $result['status'] = false;
2028                                $result['msg']  = $this->functions->lang('Error on function') . ' ldap_functions->save_institutional_accounts: ldap_rename';
2029                                $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
2030                        }
2031                }
2032               
2033                $info = array();
2034                $info['cn']                                     = $params['cn'];
2035                $info['sn']                                     = $params['cn'];
2036                $info['uid']                            = $uid;
2037                $info['mail']                           = $params['mail'];
2038               
2039                if ($params['accountStatus'] == 'on')
2040                        $info['accountStatus'] = 'active';
2041                else
2042                        $info['accountStatus'] = array();
2043               
2044                if ($params['phpgwAccountVisible'] == 'on')
2045                        $info['phpgwAccountVisible'] = '-1';
2046                else
2047                        $info['phpgwAccountVisible'] = array();
2048               
2049                if ($params['description'] != '')
2050                        $info['description'] = utf8_encode($params['description']);
2051                else
2052                        $info['description'] = array();
2053               
2054                if (!empty($params['owners']))
2055                {
2056                        foreach($params['owners'] as $index=>$uidnumber)
2057                        {
2058                                $mailForwardingAddress = $this->uidnumber2mail($uidnumber);
2059                                if ($mailForwardingAddress != '')
2060                                        $info['mailForwardingAddress'][] = $mailForwardingAddress;
2061                        }
2062                }
2063                else
2064                        $info['mailForwardingAddress'] = array();
2065               
2066                if (!@ldap_modify ( $this->ldap, $dn, $info ))
2067                {
2068                        $result['status'] = false;
2069                        $result['msg']  = $this->functions->lang('Error on function') . ' ldap_functions->save_institutional_accounts: ldap_modify';
2070                        $result['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
2071                }
2072
2073                return $result;
2074        }
2075       
2076        function get_institutional_accounts($params)
2077        {
2078                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'list_institutional_accounts'))
2079                {
2080                        $return['status'] = false;
2081                        $return['msg'] = $this->functions->lang('You do not have right to list institutional accounts') . ".";
2082                        return $return;
2083                }
2084
2085                $input = $params['input'];
2086                $justthese = array("cn", "mail", "uid");
2087                $trs = array();
2088                               
2089                foreach ($this->manager_contexts as $idx=>$context)
2090                {
2091                $institutional_accounts = ldap_search($this->ldap, $context, ("(&(phpgwAccountType=i)(|(mail=$input*)(cn=*$input*)))"), $justthese);
2092                $entries = ldap_get_entries($this->ldap, $institutional_accounts);
2093               
2094                        for ($i=0; $i<$entries['count']; $i++)
2095                        {
2096                                $tr = "<tr class='normal' onMouseOver=this.className='selected' onMouseOut=this.className='normal'><td onClick=edit_institutional_account('".$entries[$i]['uid'][0]."')>" . $entries[$i]['cn'][0] . "</td><td onClick=edit_institutional_account('".$entries[$i]['uid'][0]."')>" . $entries[$i]['mail'][0] . "</td><td align='center' onClick=delete_institutional_accounts('".$entries[$i]['uid'][0]."')><img HEIGHT='16' WIDTH='16' src=./expressoAdmin1_2/templates/default/images/delete.png></td></tr>";
2097                                $trs[$tr] = $entries[$i]['cn'][0];
2098                        }
2099                }
2100       
2101        $trs_string = '';
2102        if (count($trs))
2103        {
2104                natcasesort($trs);
2105                foreach ($trs as $tr=>$cn)
2106                {
2107                        $trs_string .= $tr;
2108                }
2109        }
2110       
2111        $return['status'] = 'true';
2112        $return['trs'] = $trs_string;
2113        return $return;
2114        }
2115       
2116        function get_institutional_account_data($params)
2117        {
2118                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'edit_institutional_accounts'))
2119                {
2120                        $return['status'] = false;
2121                        $return['msg'] = $this->functions->lang('You do not have right to list institutional accounts') . ".";
2122                        return $return;
2123                }
2124               
2125                $uid = $params['uid'];
2126                //$justthese = array("accountStatus", "phpgwAccountVisible", "cn", "mail", "mailForwardingAddress", "description");
2127                               
2128        $institutional_accounts = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], ("(&(phpgwAccountType=i)(uid=$uid))"));
2129        $entrie = ldap_get_entries($this->ldap, $institutional_accounts);
2130               
2131                if ($entrie['count'] != 1)
2132                {
2133                        $return['status'] = 'false';
2134                        $result['msg'] = $this->functions->lang('Problems loading datas') . '.';
2135                }
2136                else
2137                {
2138                        $tmp_user_context = split(",", $entrie[0]['dn']);
2139                        $tmp_reverse_user_context = array_reverse($tmp_user_context);
2140                        array_pop($tmp_reverse_user_context);
2141                        $return['user_context'] = implode(",", array_reverse($tmp_reverse_user_context));
2142                       
2143                        $return['status'] = 'true';
2144                        $return['accountStatus']                = $entrie[0]['accountstatus'][0];
2145                        $return['phpgwAccountVisible']  = $entrie[0]['phpgwaccountvisible'][0];
2146                        $return['cn']                                   = $entrie[0]['cn'][0];
2147                        $return['mail']                                 = $entrie[0]['mail'][0];
2148                        $return['description']                  = utf8_decode($entrie[0]['description'][0]);
2149
2150                        if ($entrie[0]['mailforwardingaddress']['count'] > 0)
2151                        {
2152                                $a_cn = array();
2153                                for ($i=0; $i<$entrie[0]['mailforwardingaddress']['count']; $i++)
2154                                {
2155                                        $tmp = $this->mailforwardingaddress2uidnumber($entrie[0]['mailforwardingaddress'][$i]);
2156                                        if (!$tmp) {}
2157                                        else
2158                                                $a_cn[$tmp['uidnumber']] = $tmp['cn'];
2159                                }
2160                                natcasesort($a_cn);
2161                                foreach($a_cn as $uidnumber => $cn)
2162                                {
2163                                        $return['owners'] .= '<option value='. $uidnumber .'>' . $cn . '</option>';
2164                                }
2165                        }
2166                }
2167               
2168                return $return;
2169        }
2170       
2171        function mailforwardingaddress2uidnumber($mail)
2172        {
2173                $justthese = array("uidnumber","cn");
2174        $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], ("(&(phpgwAccountType=u)(mail=$mail))"), $justthese);
2175        $entrie = ldap_get_entries($this->ldap, $search);
2176                if ($entrie['count'] != 1)
2177                        return false;
2178                else
2179                {
2180                        $return['uidnumber'] = $entrie[0]['uidnumber'][0];
2181                        $return['cn'] = $entrie[0]['cn'][0];
2182                        return $return;
2183                }
2184        }
2185       
2186        function delete_institutional_account_data($params)
2187        {
2188                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'remove_institutional_accounts'))
2189                {
2190                        $return['status'] = false;
2191                        $return['msg'] = $this->functions->lang('You do not have right to delete institutional accounts') . ".";
2192                        return $return;
2193                }
2194
2195                $uid = $params['uid'];
2196                $return['status'] = true;
2197                               
2198                $justthese = array("cn");
2199        $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], ("(&(phpgwAccountType=i)(uid=$uid))"), $justthese);
2200        $entrie = ldap_get_entries($this->ldap, $search);
2201                if ($entrie['count'] > 1)
2202                {
2203                        $return['status'] = false;
2204                        $return['msg']  = $this->functions->lang('More then one uid was found');
2205                        return $return;
2206                }               
2207                if ($entrie['count'] == 0)
2208                {
2209                        $return['status'] = false;
2210                        $return['msg']  = $this->functions->lang('No uid was found');
2211                        return $return;
2212                }               
2213               
2214                $dn = $entrie[0]['dn'];
2215                if (!@ldap_delete($this->ldap, $dn))
2216                {
2217                        $return['status'] = false;
2218                        $return['msg']  = $this->functions->lang('Error on function') . " ldap_functions->delete_institutional_accounts: ldap_delete";
2219                        $return['msg'] .= "\n" . $this->functions->lang('Server return') . ': ' . ldap_error($this->ldap);
2220                        return $return;
2221                }
2222               
2223                return $return;
2224        }
2225       
2226        function replace_mail_from_institutional_account($newMail, $oldMail)
2227        {
2228                $filter = "(&(phpgwAccountType=i)(mailforwardingaddress=$oldMail))";
2229                $justthese = array("dn");
2230                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
2231                $entries = ldap_get_entries($this->ldap, $search);
2232                $result['status'] = true;
2233                for ($i=0; $i<$entries['count']; $i++)
2234                {
2235                        $attrs['mailforwardingaddress'] = $oldMail;
2236                        $res1 = @ldap_mod_del($this->ldap, $entries[$i]['dn'], $attrs);
2237                        $attrs['mailforwardingaddress'] = $newMail;
2238                        $res2 = @ldap_mod_add($this->ldap, $entries[$i]['dn'], $attrs);
2239               
2240                        if ((!$res1) || (!$res2))
2241                        {
2242                                $result['status'] = false;
2243                                $return['msg']  = $this->functions->lang('Error on function') . " ldap_functions->replace_mail_from_institutional_account.";
2244                        }
2245                }
2246               
2247                return $result;
2248        }
2249}
2250?>
Note: See TracBrowser for help on using the repository browser.