source: companies/celepar/expressoAdmin1_2/inc/class.ldap_functions.inc.php @ 763

Revision 763, 76.9 KB checked in by niltonneto, 15 years ago (diff)

Importação inicial do Expresso da Celepar

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" => lang("Login generator disabled"));
378               
379                if( (isset($this->current_config['expressoAdmin_loginGenScript'])) &&
380                                ($this->current_config['expressoAdmin_loginGenScript'])) {
381                       
382                        include_once "if.login.inc.php";
383                        include_once "class.".$this->current_config['expressoAdmin_loginGenScript'].
384                                        ".inc.php";
385
386                        $classe = new ReflectionClass($this->current_config['expressoAdmin_loginGenScript']);
387                                       
388                        if(!$classe->implementsInterface('login'))
389                        {
390                                return array(
391                                        "status" => False,
392                                        "msg" => lang("Login interface not implemented (contact suport)")
393                                );
394                        }
395
396                        $login = $classe->newInstance()->generate_login($params["first_name"],$params["second_name"],$this->ldap);
397                       
398                        /*
399                                If login exists, it concatenates a number to the end.
400                                resulting in a new login
401                         */
402                        $i = 1;
403                        while($i < 1000) // Limit of 1000 equal names
404                        {
405                                $search = ldap_search($this->ldap, $context, "(uid=".$login.")", $justthese);
406                                if (ldap_count_entries($this->ldap,$search) == 0)
407                                        break;
408                                else
409                                {
410                                        if ($i > 1) // If login have a number, remove the number and put the new one
411                                                $login=substr($login,0,strlen($login)-strlen($i)).$i;
412                                        else
413                                                $login.=$i;
414                                        $i++;
415                                }
416                        }
417                }
418               
419                return array('status'=>true,'msg' => $login);
420        }
421        function validate_fields_group($params)
422        {
423                /* ldap connection following referals and using Contac Center config*/
424                if (is_array($_SESSION['phpgw_info']['expresso']['cc_ldap_server']))
425                {
426                        $ldap_connection = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host']);
427                        if ($ldap_connection)
428                        {
429                                ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
430                                ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
431                                if ( ($GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
432                                        ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['acc'], $GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['pw']);
433                                $context = $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['dn'];
434                        }
435                        else
436                        {
437                                $result['status'] = false;
438                                $result['msg'] = $this->functions->lang('Connection with ldap fail') . ".";
439                                return $result;
440                        }
441                }
442                else
443                {
444                        $ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
445                        ldap_set_option($ldap_connection,LDAP_OPT_PROTOCOL_VERSION,3);
446                        ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
447                        ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']);
448                        $context = $GLOBALS['phpgw_info']['server']['ldap_context'];
449                }
450
451                $cn = $params['cn'];
452                $result['status'] = true;
453               
454                if ($_SESSION['phpgw_info']['expresso']['global_denied_groups'][$cn])
455                {
456                        $result['status'] = false;
457                        $result['msg'] = $this->functions->lang('This group name can not be used because is a System Account') . ".";
458                        return $result;
459                }
460               
461                // CN
462                $filter = "(&(phpgwAccountType=g)(cn=$cn))";
463                $justthese = array("cn");
464                $search = ldap_search($ldap_connection, $context, $filter, $justthese);
465                $count_entries = ldap_count_entries($ldap_connection,$search);
466                if ($count_entries > 0)
467                {
468                        $result['status'] = false;
469                        $result['msg'] = $this->functions->lang('This name is already used') . ".";
470                        return $result;
471                }
472               
473                // UID
474                $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uid=$cn))";
475                $justthese = array("uid");
476                $search = ldap_search($ldap_connection, $context, $filter, $justthese);
477                $count_entries = ldap_count_entries($ldap_connection,$search);
478                if ($count_entries > 0)
479                {
480                        $result['status'] = false;
481                        $result['msg'] = $this->functions->lang('This grupo name is already used by an user') . ".";
482                        return $result;
483                }
484               
485                return $result;
486        }
487       
488        function validate_fields_maillist($params)
489        {
490                /* ldap connection following referals and using Contac Center config*/
491                if (is_array($_SESSION['phpgw_info']['expresso']['cc_ldap_server']))
492                {
493                        $ldap_connection = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host']);
494                        if ($ldap_connection)
495                        {
496                                ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
497                                ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
498                                if ( ($GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
499                                        ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['acc'], $GLOBALS['phpgw_info']['expresso']['cc_ldap_server']['pw']);
500                                $context = $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['dn'];
501                        }
502                        else
503                        {
504                                $result['status'] = false;
505                                $result['msg'] = $this->functions->lang('Connection with ldap fail') . ".";
506                                return $result;
507                        }
508                }
509                else
510                {
511                        $ldap_connection = ldap_connect($GLOBALS['phpgw_info']['server']['ldap_host']);
512                        ldap_set_option($ldap_connection,LDAP_OPT_PROTOCOL_VERSION,3);
513                        ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, true);
514                        ldap_bind($ldap_connection, $GLOBALS['phpgw_info']['server']['ldap_root_dn'], $GLOBALS['phpgw_info']['server']['ldap_root_pw']);
515                        $context = $GLOBALS['phpgw_info']['server']['ldap_context'];
516                }
517               
518                $uid = $params['uid'];
519                $mail = $params['mail'];
520                $result['status'] = true;
521               
522                if ($_SESSION['phpgw_info']['expresso']['global_denied_users'][$uid])
523                {
524                        $result['status'] = false;
525                        $result['msg'] = $this->functions->lang('This LOGIN can not be used because is a System Account') . ".";
526                        return $result;
527                }
528               
529                // UID
530                $filter = "(&(phpgwAccountType=l)(uid=$uid))";
531                $justthese = array("uid");
532                $search = ldap_search($ldap_connection, $context, $filter, $justthese);
533                $count_entries = ldap_count_entries($ldap_connection,$search);
534                if ($count_entries > 0)
535                {
536                        $result['status'] = false;
537                        $result['msg'] = $this->functions->lang('this email list login is already used') . ".";
538                        return $result;
539                }
540               
541                // MAIL
542                $filter = "(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|(mail=$mail)(mailalternateaddress=$mail)))";
543                $justthese = array("mail");
544                $search = ldap_search($ldap_connection, $context, $filter, $justthese);
545                $count_entries = ldap_count_entries($ldap_connection,$search);
546                if ($count_entries > 0)
547                {
548                        $result['status'] = false;
549                        $result['msg'] = $this->functions->lang('this email address is already used') . ".";
550                        return $result;
551                }
552               
553                return $result;
554        }
555
556        //Busca usuários de um contexto e já retorna as options do select;
557        function get_available_users($params)
558        {
559                $context = $params['context'];
560                $recursive = $params['recursive'];
561                $justthese = array("cn", "uidNumber");
562                $filter="(phpgwAccountType=u)";
563               
564                if ($recursive == 'true')
565                        $groups_list=ldap_search($this->ldap, $context, $filter, $justthese);
566                else
567                $groups_list=ldap_list($this->ldap, $context, $filter, $justthese);
568       
569        $entries = ldap_get_entries($this->ldap, $groups_list);
570       
571                for ($i=0; $i<$entries["count"]; $i++){
572                        $u_tmp[$entries[$i]["uidnumber"][0]] = $entries[$i]["cn"][0];
573                }
574                       
575                if (count($u_tmp))
576                        natcasesort($u_tmp);
577
578                $i = 0;
579                $users = array();
580                       
581                if (count($u_tmp))
582                {
583                        foreach ($u_tmp as $uidnumber => $cn)
584                        {
585                                $options .= "<option value=$uidnumber>$cn</option>";
586                        }
587                        unset($u_tmp);
588                }
589
590        return $options;
591        }
592
593        //Busca usuários e listas de um contexto e já retorna as options do select;
594        function get_available_users_and_maillist($params)
595        {
596                $context = $params['context'];
597                $recursive = $params['recursive'];
598               
599                //Usado para retirar a própria lista das possibilidades de inclusão.
600                $denied_uidnumber = $params['denied_uidnumber'];
601               
602                $justthese = array("cn", "uidNumber", "mail");
603               
604                $users_filter="(phpgwAccountType=u)";
605                $lists_filter = $denied_uidnumber == '' ? "(phpgwAccountType=l)" : "(&(phpgwAccountType=l)(!(uidnumber=$denied_uidnumber)))";
606               
607                $users = Array();
608                $lists = Array();               
609
610                /* folling referral connection */
611                $ldap_conn_following_ref = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host']);
612                if ($ldap_conn_following_ref)
613                {
614                        ldap_set_option($ldap_conn_following_ref, LDAP_OPT_PROTOCOL_VERSION, 3);
615                        ldap_set_option($ldap_conn_following_ref, LDAP_OPT_REFERRALS, 1);
616
617                        if ( ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
618                                ldap_bind($ldap_conn_following_ref, $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'], $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw']);
619                }
620                else
621                        return false;
622
623                if ($recursive == 'true')
624                {
625                        $lists_search = ldap_search($ldap_conn_following_ref, $context, $lists_filter, $justthese);
626                        $users_search = ldap_search($ldap_conn_following_ref, $context, $users_filter, $justthese);
627                }
628                else
629                {
630                        $lists_search = ldap_list($ldap_conn_following_ref, $context, $lists_filter, $justthese);
631                        $users_search = ldap_list($ldap_conn_following_ref, $context, $users_filter, $justthese);
632                }
633               
634                /* email lists */
635                $lists_entries = ldap_get_entries($ldap_conn_following_ref, $lists_search);
636               
637                for ($i=0; $i<$lists_entries["count"]; $i++)
638                {
639                        $l_tmp[$lists_entries[$i]["mail"][0]] = $lists_entries[$i]["cn"][0];
640                }
641                       
642                if (count($l_tmp))
643                        natcasesort($l_tmp);
644                       
645                $i = 0;
646                $lists = array();
647               
648                $options .= '<option  value="-1" disabled>------------------------------&nbsp;&nbsp;&nbsp;&nbsp;'.$this->functions->lang('email lists').'&nbsp;&nbsp;&nbsp;&nbsp;------------------------------ </option>'."\n";       
649                if (count($l_tmp))
650                {
651                        foreach ($l_tmp as $mail => $cn)
652                        {
653                                $options .= "<option value=$mail>$cn</option>";
654                        }
655                        unset($l_tmp);
656                }
657               
658                /* users */
659                $users_entries = ldap_get_entries($ldap_conn_following_ref, $users_search);
660                for ($i=0; $i<$users_entries["count"]; $i++)
661                {
662                        $u_tmp[$users_entries[$i]["mail"][0]] = $users_entries[$i]["cn"][0];
663                }
664                       
665                if (count($u_tmp))
666                        natcasesort($u_tmp);
667                       
668                $i = 0;
669                $users = array();
670               
671                $options .= '<option  value="-1" disabled>-----------------------------&nbsp;&nbsp;&nbsp;&nbsp;'.$this->functions->lang('users').'&nbsp;&nbsp;&nbsp;&nbsp;---------------------------- </option>'."\n";
672                       
673                if (count($u_tmp))
674                {
675                        foreach ($u_tmp as $mail => $cn)
676                        {
677                                $options .= "<option value=$mail class='line-above'>$cn</option>";
678                        }
679                        unset($u_tmp);
680                }
681               
682                ldap_close($ldap_conn_following_ref);
683                return $options;
684        }
685
686        function get_available_groups($params)
687        {
688                $context = $params['context'];
689                $justthese = array("cn", "gidNumber");
690        $groups_list=ldap_list($this->ldap, $context, ("(phpgwAccountType=g)"), $justthese);
691        ldap_sort($this->ldap, $groups_list, "cn");
692       
693        $entries = ldap_get_entries($this->ldap, $groups_list);
694               
695                $options = '';
696                for ($i=0; $i<$entries['count']; $i++)
697                {
698                        $options .= "<option value=" . $entries[$i]['gidnumber'][0] . ">" . $entries[$i]['cn'][0] . "</option>";
699                }
700       
701        return $options;               
702        }
703       
704        function get_available_maillists($params)
705        {
706                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
707                        return false;
708               
709                $context = $params['context'];
710                $justthese = array("uid","mail","uidNumber");
711        $maillists=ldap_list($ldapMasterConnect, $context, ("(phpgwAccountType=l)"), $justthese);
712        ldap_sort($ldapMasterConnect, $maillists, "uid");
713       
714        $entries = ldap_get_entries($ldapMasterConnect, $maillists);
715       
716                $options = '';                 
717                for ($i=0; $i<$entries['count']; $i++)
718                {
719                        $options .= "<option value=" . $entries[$i]['uid'][0] . ">" . $entries[$i]['uid'][0] . " (" . $entries[$i]['mail'][0] . ")" . "</option>";
720                }
721       
722        ldap_close($ldapMasterConnect);
723        return $options;
724        }
725       
726        function ldap_add_entry($dn, $entry)
727        {
728                $result = array();
729                if (!@ldap_add ( $this->ldap, $dn, $entry ))
730                {
731                        $result['status']               = false;
732                        $result['error_number'] = ldap_errno($this->ldap);
733                        $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);
734                }
735                else
736                        $result['status'] = true;
737               
738                return $result;
739        }
740       
741        function ldap_save_photo($dn, $pathphoto, $photo_exist=false)
742        {
743                $fd = fopen($pathphoto, "r");
744                $fsize = filesize($pathphoto);
745                $jpegStr = fread($fd, $fsize);
746                fclose ($fd);
747                $attrs['jpegPhoto'] = $jpegStr;
748                       
749                if ($photo_exist)
750                        $res = @ldap_mod_replace($this->ldap, $dn, $attrs);
751                else
752                        $res = @ldap_mod_add($this->ldap, $dn, $attrs);
753                       
754                if ($res)
755                {
756                        $result['status'] = true;
757                }
758                else
759                {
760                        $result['status'] = false;
761                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->ldap_save_photo ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
762                }
763               
764                return $result;
765        }
766       
767        function ldap_remove_photo($dn)
768        {
769                $attrs['jpegPhoto'] = array();
770                $res = ldap_mod_del($this->ldap, $dn, $attrs);
771               
772                if ($res)
773                {
774                        $result['status'] = true;
775                }
776                else
777                {
778                        $result['status'] = false;
779                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->ldap_remove_photo ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
780                }
781               
782                return $result;
783        }       
784       
785        // Pode receber tanto um único memberUid quanto um array de memberUid's
786        function add_user2group($gidNumber, $memberUid)
787        {
788                $filter = "(&(phpgwAccountType=g)(gidNumber=$gidNumber))";
789                $justthese = array("dn");
790                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
791                $entry = ldap_get_entries($this->ldap, $search);
792                $group_dn = $entry[0]['dn'];
793                $attrs['memberUid'] = $memberUid;
794               
795                $res = @ldap_mod_add($this->ldap, $group_dn, $attrs);
796               
797                if ($res)
798                {
799                        $result['status'] = true;
800                }
801                else
802                {
803                        $result['status'] = false;
804                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user2group ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
805                }
806                return $result;
807        }
808       
809        function remove_user2group($gidNumber, $memberUid)
810        {
811                $filter = "(&(phpgwAccountType=g)(gidNumber=$gidNumber))";
812                $justthese = array("dn");
813                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
814                $entry = ldap_get_entries($this->ldap, $search);
815                $group_dn = $entry[0]['dn'];
816                $attrs['memberUid'] = $memberUid;
817                $res = @ldap_mod_del($this->ldap, $group_dn, $attrs);
818               
819                if ($res)
820                {
821                        $result['status'] = true;
822                }
823                else
824                {
825                        $result['status'] = false;
826                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->remove_user2group ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
827                }
828                return $result;
829        }
830       
831        function add_user2maillist($uid, $mail)
832        {
833                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
834                {
835                        $result['status'] = false;
836                        $result['msg'] = $this->functions->lang('Ldap connection fail') . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
837                        return $result;
838                }
839                       
840                $filter = "(&(phpgwAccountType=l)(uid=$uid))";
841                $justthese = array("dn");
842                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
843                $entry = ldap_get_entries($ldapMasterConnect, $search);
844                $group_dn = $entry[0]['dn'];
845                $attrs['mailForwardingAddress'] = $mail;
846                $res = @ldap_mod_add($ldapMasterConnect, $group_dn, $attrs);
847               
848                if ($res)
849                {
850                        $result['status'] = true;
851                }
852                else
853                {
854                        $result['status'] = false;
855                        if (ldap_errno($ldapMasterConnect) == '50')
856                        {
857                                $result['msg'] =        $this->functions->lang('Error on the function') . ' ldap_functions->add_user2maillist' . ".\n" .
858                                                                        $this->functions->lang('The user used for record on LPDA, must have write access') . ".\n";
859                                                                        $this->functions->lang('The user') . ' ' . $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] . ' ' . $this->functions->lang('does not have this access') . ".\n";
860                                                                        $this->functions->lang('Edit Global Catalog Config, in the admin module, and add an user with write access') . ".\n";
861                        }                                       
862                        else
863                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user2maillist ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
864                }
865               
866                ldap_close($ldapMasterConnect);
867                return $result;
868        }
869       
870        function add_user2maillist_scl($dn, $array_emails)
871        {
872                $attrs['mailSenderAddress'] = $array_emails;
873               
874                $res = @ldap_mod_add($this->ldap, $dn, $attrs);
875               
876                if ($res)
877                {
878                        $result['status'] = true;
879                }
880                else
881                {
882                        $result['status'] = false;
883                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user2maillist_scp ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
884                }
885                return $result;
886        }
887
888        function remove_user2maillist($uid, $mail)
889        {
890                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
891                {
892                        $result['status'] = false;
893                        $result['msg'] = $this->functions->lang('Ldap connection fail') . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
894                        return $result;
895                }
896               
897                $filter = "(&(phpgwAccountType=l)(uid=$uid))";
898                $justthese = array("dn");
899                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
900                $entry = ldap_get_entries($ldapMasterConnect, $search);
901                $group_dn = $entry[0]['dn'];
902                $attrs['mailForwardingAddress'] = $mail;
903                $res = @ldap_mod_del($ldapMasterConnect, $group_dn, $attrs);
904               
905                if ($res)
906                {
907                        $result['status'] = true;
908                }
909                else
910                {
911                        $result['status'] = false;
912                        if (ldap_errno($ldapMasterConnect) == '50')
913                        {
914                                $result['msg'] =        $this->functions->lang('Error on the function') . ' ldap_functions->remove_user2maillist' . ".\n" .
915                                                                        $this->functions->lang('The user used for record on LPDA, must have write access') . ".\n";
916                                                                        $this->functions->lang('The user') . ' ' . $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] . ' ' . $this->functions->lang('does not have this access') . ".\n";
917                                                                        $this->functions->lang('Edit Global Catalog Config, in the admin module, and add an user with write access') . ".\n";
918                        }                                       
919                        else
920                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->remove_user2maillist ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
921                }
922                ldap_close($ldapMasterConnect);
923                return $result;
924        }
925
926        function remove_user2maillist_scl($dn, $array_emails)
927        {
928                $attrs['mailSenderAddress'] = $array_emails;
929                $res = @ldap_mod_del($this->ldap, $dn, $attrs);
930               
931                if ($res)
932                {
933                        $result['status'] = true;
934                }
935                else
936                {
937                        $result['status'] = false;
938                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->remove_user2maillist_scp ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
939                }
940                return $result;
941        }
942
943        function replace_user2maillists($new_mail, $old_mail)
944        {
945                $filter = "(&(phpgwAccountType=l)(mailforwardingaddress=$old_mail))";
946                $justthese = array("dn");
947                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
948                $entries = ldap_get_entries($this->ldap, $search);
949                $result['status'] = true;
950                for ($i=0; $i<$entries['count']; $i++)
951                {
952                        $attrs['mailforwardingaddress'] = $old_mail;
953                        $res1 = @ldap_mod_del($this->ldap, $entries[$i]['dn'], $attrs);
954                        $attrs['mailforwardingaddress'] = $new_mail;
955                        $res2 = @ldap_mod_add($this->ldap, $entries[$i]['dn'], $attrs);
956               
957                        if ((!$res1) || (!$res2))
958                        {
959                                $result['status'] = false;
960                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->replace_user2maillists ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
961                        }
962                }
963               
964                return $result;
965        }
966       
967        function get_user_info($uidnumber)
968        {
969                foreach ($this->manager_contexts as $index=>$context)
970                {
971                        $filter="(&(phpgwAccountType=u)(uidNumber=".$uidnumber."))";
972                        $search = ldap_search($this->ldap, $context, $filter);
973                        $entry = ldap_get_entries($this->ldap, $search);
974                       
975                        if ($entry['count'])
976                        {
977                                //Pega o dn do setor do usuario.
978                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
979                                $sector_dn_array = explode(",", $entry[0]['dn']);
980                                for($i=1; $i<count($sector_dn_array); $i++)
981                                        $sector_dn .= $sector_dn_array[$i] . ',';
982                                //Retira ultimo pipe.
983                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
984               
985                                $result['context']                              = $sector_dn;
986                                $result['uid']                                  = $entry[0]['uid'][0];
987                                $result['uidnumber']                    = $entry[0]['uidnumber'][0];
988                                $result['gidnumber']                    = $entry[0]['gidnumber'][0];
989                                $result['departmentnumber']             = $entry[0]['departmentnumber'][0];
990                                $result['givenname']                    = $entry[0]['givenname'][0];
991                                $result['sn']                                   = $entry[0]['sn'][0];
992                                $result['telephonenumber']              = $entry[0]['telephonenumber'][0];
993                                $result['passwd_expired']               = $entry[0]['phpgwlastpasswdchange'][0];
994                                $result['phpgwaccountstatus']   = $entry[0]['phpgwaccountstatus'][0];
995                                $result['phpgwaccountvisible']  = $entry[0]['phpgwaccountvisible'][0];
996                                $result['accountstatus']                = $entry[0]['accountstatus'][0];
997                                $result['mail']                                 = $entry[0]['mail'][0];
998                                $result['mailalternateaddress'] = $entry[0]['mailalternateaddress'];
999                                $result['mailforwardingaddress']= $entry[0]['mailforwardingaddress'];
1000                                $result['deliverymode']                 = $entry[0]['deliverymode'][0];
1001                                $result['userPasswordRFC2617']  = $entry[0]['userpasswordrfc2617'][0];
1002
1003                                //Photo
1004                                if ($entry[0]['jpegphoto']['count'] == 1)
1005                                        $result['photo_exist'] = 'true';
1006               
1007                                // Samba
1008                                for ($i=0; $i<$entry[0]['objectclass']['count']; $i++)
1009                                {
1010                                        if ($entry[0]['objectclass'][$i] == 'sambaSamAccount')
1011                                                $result['sambaUser'] = true;
1012                                }
1013                                if (($this->current_config['expressoAdmin_samba_support'] == 'true') && ($result['sambaUser']))
1014                                {
1015                                        $result['sambaaccflags'] = $entry[0]['sambaacctflags'][0];
1016                                        $result['sambalogonscript'] = $entry[0]['sambalogonscript'][0];
1017                                        $result['homedirectory'] = $entry[0]['homedirectory'][0];
1018                                        $a_tmp = explode("-", $entry[0]['sambasid'][0]);
1019                                        array_pop($a_tmp);
1020                                        $result['sambasid'] = implode("-", $a_tmp);
1021                                }
1022
1023                                // Verifica o acesso do gerente aos atributos corporativos
1024                                if ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'manipulate_corporative_information'))
1025                                {
1026                                        $result['corporative_information_employeenumber']= $entry[0]['employeenumber'][0];
1027                                        $result['corporative_information_cpf']                  = $entry[0]['cpf'][0];
1028                                        $result['corporative_information_rg']                   = $entry[0]['rg'][0];
1029                                        $result['corporative_information_rguf']                 = $entry[0]['rguf'][0];
1030                                        $result['corporative_information_description']  = utf8_decode($entry[0]['description'][0]);
1031                                }
1032                               
1033                                // MailLists
1034                                $result['maillists_info'] = $this->get_user_maillists($result['mail']);
1035                                if($result['maillists_info'])
1036                                {
1037                                        foreach ($result['maillists_info'] as $maillist)
1038                                        {
1039                                                $result['maillists'][] = $maillist['uid'];
1040                                        }
1041                                }
1042                               
1043                                // Groups
1044                                $justthese = array("gidnumber","cn");
1045                                $filter="(&(phpgwAccountType=g)(memberuid=".$result['uid']."))";
1046                                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1047                        ldap_sort($this->ldap, $search, "cn");
1048                        $entries = ldap_get_entries($this->ldap, $search);
1049                        for ($i=0; $i<$entries['count']; $i++)
1050                        {
1051                                $result['groups_ldap'][ $entries[$i]['gidnumber'][0] ] = $entries[$i]['cn'][0];
1052                        }
1053                        }
1054                }
1055                if (is_array($result))
1056                        return $result;
1057                else
1058                        return false;
1059        }
1060               
1061        function get_user_maillists($mail)
1062        {
1063                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
1064                        return false;
1065               
1066                $result = array();
1067               
1068                //Mostra somente os mailists dos contextos do gerente
1069                $justthese = array("uid","mail","uidnumber");
1070                $filter="(&(phpgwAccountType=l)(mailforwardingaddress=$mail))";
1071               
1072                foreach ($this->manager_contexts as $index=>$context)
1073                {
1074                        $search = ldap_search($ldapMasterConnect, $context, $filter, $justthese);
1075                $entries = ldap_get_entries($ldapMasterConnect, $search);
1076               
1077                for ($i=0; $i<$entries['count']; $i++)
1078                {
1079                                $result[ $entries[$i]['uid'][0] ]['uid']                = $entries[$i]['uid'][0];
1080                                $result[ $entries[$i]['uid'][0] ]['mail']               = $entries[$i]['mail'][0];
1081                               
1082                                $a_tmp[] = $entries[$i]['uid'][0];
1083                }
1084                }
1085       
1086        if($a_tmp) {
1087                natcasesort($a_tmp);
1088       
1089                foreach ($a_tmp as $uid)
1090                {
1091                                $return[$uid]['uid']            = $result[$uid]['uid'];
1092                                $return[$uid]['mail']           = $result[$uid]['mail'];
1093                }
1094        }
1095        ldap_close($ldapMasterConnect);
1096                return $return;
1097        }
1098       
1099        function get_group_info($gidnumber)
1100        {
1101                foreach ($this->manager_contexts as $index=>$context)
1102                {
1103                        $filter="(&(phpgwAccountType=g)(gidNumber=".$gidnumber."))";
1104                        $search = ldap_search($this->ldap, $context, $filter);
1105                        $entry = ldap_get_entries($this->ldap, $search);
1106                       
1107                        if ($entry['count'])
1108                        {
1109                                //Pega o dn do setor do grupo.
1110                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
1111                                $sector_dn_array = explode(",", $entry[0]['dn']);
1112                                for($i=1; $i<count($sector_dn_array); $i++)
1113                                        $sector_dn .= $sector_dn_array[$i] . ',';
1114                                //Retira ultimo pipe.
1115                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
1116               
1117                                $result['context']                              = $sector_dn;
1118                                $result['cn']                                   = $entry[0]['cn'][0];
1119                                $result['description']                  = $entry[0]['description'][0];
1120                                $result['gidnumber']                    = $entry[0]['gidnumber'][0];
1121                                $result['phpgwaccountvisible']  = $entry[0]['phpgwaccountvisible'][0];
1122                                $result['email']                                = $entry[0]['mail'][0];
1123               
1124                                //MemberUid
1125                                for ($i=0; $i<$entry[0]['memberuid']['count']; $i++)
1126                                {
1127                                        $justthese = array("cn","uid","uidnumber");
1128                       
1129                                        // Montagem dinamica do filtro
1130                                        $filter="(&(phpgwAccountType=u)(|";
1131                                        for ($k=0; (($k<10) && ($i<$entry[0]['memberuid']['count'])); $k++)
1132                                        {
1133                                                $filter .= "(uid=".$entry[0]['memberuid'][$i].")";
1134                                                $i++;
1135                                        }
1136                                        $i--;
1137                                        $filter .= "))";
1138                       
1139                                        $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1140                                        $user_entry = ldap_get_entries($this->ldap, $search);
1141
1142                                        for ($j=0; $j<$user_entry['count']; $j++)
1143                                        {
1144                                                $result['memberuid_info'][$user_entry[$j]['uid'][0]]['cn'] = $user_entry[$j]['cn'][0];
1145                                                $result['memberuid_info'][$user_entry[$j]['uid'][0]]['uidnumber'] = $user_entry[$j]['uidnumber'][0];
1146                                                $result['memberuid_info'][$user_entry[$j]['uid'][0]]['type'] = 'u';
1147                                        }
1148                                }
1149               
1150                                // Checamos e-mails que não fazem parte do expresso.
1151                                // Criamos um array temporario
1152                                $tmp_array = array();
1153                                if($result['memberuid_info'])
1154                                        foreach ($result['memberuid_info'] as $uid => $user_data)
1155                                        {
1156                                                $tmp_array[] = $uid;
1157                                        }
1158               
1159                                if($entry[0]['memberuid']) {
1160                                        // Retira o count do array
1161                                        array_shift($entry[0]['memberuid']);
1162                                        // Vemos a diferença
1163                                        $array_diff = array_diff($entry[0]['memberuid'], $tmp_array);
1164                                        // Incluimos no resultado                       
1165                                        foreach ($array_diff as $index=>$uid)
1166                                        {
1167                                                $result['memberuid_info'][$uid]['cn'] = $uid;
1168                                        }
1169                                }
1170               
1171                                // Samba
1172                                for ($i=0; $i<$entry[0]['objectclass']['count']; $i++)
1173                                {
1174                                        if ($entry[0]['objectclass'][$i] == 'sambaGroupMapping')
1175                                                $result['sambaGroup'] = true;
1176
1177                                        $a_tmp = explode("-", $entry[0]['sambasid'][0]);
1178                                        array_pop($a_tmp);
1179                                        $result['sambasid'] = implode("-", $a_tmp);
1180                                }
1181                                return $result;
1182                        }
1183                }
1184        }       
1185       
1186        function get_maillist_info($uidnumber)
1187        {
1188                /* folling referral connection */
1189                $ldap_conn_following_ref = ldap_connect($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['host']);
1190                if ($ldap_conn_following_ref)
1191                {
1192                        ldap_set_option($ldap_conn_following_ref, LDAP_OPT_PROTOCOL_VERSION, 3);
1193                        ldap_set_option($ldap_conn_following_ref, LDAP_OPT_REFERRALS, 1);
1194
1195                        if ( ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] != '') && ($_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw'] != '') )
1196                                ldap_bind($ldap_conn_following_ref, $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'], $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['pw']);
1197                }
1198               
1199                foreach ($this->manager_contexts as $index=>$context)
1200                {
1201                        $filter="(&(phpgwAccountType=l)(uidNumber=".$uidnumber."))";
1202                        $search = ldap_search($this->ldap, $context, $filter);
1203                        $entry = ldap_get_entries($this->ldap, $search);
1204                       
1205                        if ($entry['count'])
1206                        {
1207                                //Pega o dn do setor do usuario.
1208                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
1209                                $sector_dn_array = explode(",", $entry[0]['dn']);
1210                                for($i=1; $i<count($sector_dn_array); $i++)
1211                                        $sector_dn .= $sector_dn_array[$i] . ',';
1212                                //Retira ultimo pipe.
1213                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
1214                       
1215                                $result['context']                              = $sector_dn;
1216                                $result['uidnumber']                    = $entry[0]['uidnumber'][0];
1217                                $result['uid']                                  = strtolower($entry[0]['uid'][0]);
1218                                $result['cn']                                   = $entry[0]['cn'][0];
1219                                $result['mail']                                 = $entry[0]['mail'][0];
1220                                $result['description']                  = utf8_decode($entry[0]['description'][0]);
1221                                $result['accountStatus']                = $entry[0]['accountstatus'][0];
1222                                $result['phpgwAccountVisible']  = $entry[0]['phpgwaccountvisible'][0];
1223                       
1224                                //Members
1225                                for ($i=0; $i<$entry[0]['mailforwardingaddress']['count']; $i++)
1226                                {
1227                                        $justthese = array("cn", "uidnumber", "uid", "phpgwaccounttype", "mail");
1228                               
1229                                        // Montagem dinamica do filtro, para nao ter muitas conexoes com o ldap
1230                                        $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(|";
1231                                        for ($k=0; (($k<10) && ($i<$entry[0]['mailforwardingaddress']['count'])); $k++)
1232                                        {
1233                                                $filter .= "(mail=".$entry[0]['mailforwardingaddress'][$i].")";
1234                                                $i++;
1235                                        }
1236                                        $i--;
1237                                        $filter .= "))";
1238                               
1239                                        $search = ldap_search($ldap_conn_following_ref, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1240                                        $user_entry = ldap_get_entries($ldap_conn_following_ref, $search);
1241                                                                       
1242                                        for ($j=0; $j<$user_entry['count']; $j++)
1243                                        {
1244                                                $result['mailForwardingAddress_info'][$user_entry[$j]['mail'][0]]['uid'] = $user_entry[$j]['uid'][0];
1245                                                $result['mailForwardingAddress_info'][$user_entry[$j]['mail'][0]]['cn'] = $user_entry[$j]['cn'][0];
1246                                                $result['mailForwardingAddress_info'][$user_entry[$j]['mail'][0]]['type'] = $user_entry[$j]['phpgwaccounttype'][0];
1247                                                $result['mailForwardingAddress'][] = $user_entry[$j]['mail'][0];
1248                                        }
1249                                }
1250
1251                                // Emails não encontrados no ldap
1252                                array_shift($entry[0]['mailforwardingaddress']); //Retira o count do array
1253                                $missing_emails = array_diff($entry[0]['mailforwardingaddress'], $result['mailForwardingAddress']);
1254                               
1255                                // Incluimos estes no resultado
1256                                foreach ($missing_emails as $index=>$mailforwardingaddress)
1257                                {
1258                                        $result['mailForwardingAddress_info'][$mailforwardingaddress]['uid'] = $mailforwardingaddress;
1259                                        $result['mailForwardingAddress_info'][$mailforwardingaddress]['cn'] = 'E-Mail nao encontrado';
1260                                        $result['mailForwardingAddress'][] = $mailforwardingaddress;
1261                                }
1262                               
1263                                ldap_close($ldap_conn_following_ref);
1264                                return $result;
1265                        }
1266                }
1267        }       
1268
1269        function get_maillist_scl_info($uidnumber)
1270        {
1271                foreach ($this->manager_contexts as $index=>$context)
1272                {
1273                        $filter="(&(phpgwAccountType=l)(uidNumber=$uidnumber))";
1274                        $search = ldap_search($this->ldap, $context, $filter);
1275                        $entry = ldap_get_entries($this->ldap, $search);
1276
1277                        if ($entry['count'])
1278                        {
1279                                //Pega o dn do setor do usuario.
1280                                $entry[0]['dn'] = strtolower($entry[0]['dn']);
1281                                $sector_dn_array = explode(",", $entry[0]['dn']);
1282                                for($i=1; $i<count($sector_dn_array); $i++)
1283                                        $sector_dn .= $sector_dn_array[$i] . ',';
1284                                //Retira ultimo pipe.
1285                                $sector_dn = substr($sector_dn,0,(strlen($sector_dn) - 1));
1286               
1287                                $result['dn']                                           = $entry[0]['dn'];
1288                                $result['context']                                      = $sector_dn;
1289                                $result['uidnumber']                            = $entry[0]['uidnumber'][0];
1290                                $result['uid']                                          = $entry[0]['uid'][0];
1291                                $result['cn']                                           = $entry[0]['cn'][0];
1292                                $result['mail']                                         = $entry[0]['mail'][0];
1293                                $result['accountStatus']                        = $entry[0]['accountstatus'][0];
1294                                $result['phpgwAccountVisible']          = $entry[0]['phpgwaccountvisible'][0];
1295                                $result['accountRestrictive']           = $entry[0]['accountrestrictive'][0];
1296                                $result['participantCanSendMail']       = $entry[0]['participantcansendmail'][0];
1297               
1298                                //Senders
1299                                for ($i=0; $i<$entry[0]['mailsenderaddress']['count']; $i++)
1300                                {
1301                                        $justthese = array("cn", "uidnumber", "uid", "mail");
1302                                        $filter="(&(phpgwAccountType=u)(mail=".$entry[0]['mailsenderaddress'][$i]."))";
1303                                        $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1304                                        $user_entry = ldap_get_entries($this->ldap, $search);
1305                       
1306                                        $result['senders_info'][$user_entry[0]['mail'][0]]['uid'] = $user_entry[0]['uid'][0];
1307                                        $result['senders_info'][$user_entry[0]['mail'][0]]['cn'] = $user_entry[0]['cn'][0];
1308                                        $result['members'][] = $user_entry[0]['mail'][0];
1309                                }
1310                                return $result;
1311                        }
1312                }
1313        }       
1314
1315        function group_exist($gidnumber)
1316        {
1317                $justthese = array("cn");
1318                $filter="(&(phpgwAccountType=g)(gidNumber=".$gidnumber."))";
1319                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1320                               
1321                $entry = ldap_get_entries($this->ldap, $search);
1322                if ($entry['count'] == 0)
1323                        return false;
1324                else
1325                        return true;
1326        }
1327
1328        function gidnumbers2cn($gidnumbers)
1329        {
1330                $result = array();
1331                if (count($gidnumbers))
1332                {
1333                        $justthese = array("cn");
1334                        $i = 0;
1335                        foreach ($gidnumbers as $gidnumber)
1336                        {
1337                                $filter="(&(phpgwAccountType=g)(gidNumber=".$gidnumber."))";
1338                                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1339                               
1340                                $entry = ldap_get_entries($this->ldap, $search);
1341                                if ($entry['count'] == 0)
1342                                        $result['groups_info'][$i]['cn'] = '_' . $this->functions->lang('group only exist on DB, but does not exist on ldap');
1343                                       
1344                                else
1345                                        $result['groups_info'][$i]['cn'] = $entry[0]['cn'][0];
1346                                $result['groups_info'][$i]['gidnumber'] = $gidnumber;
1347                       
1348                                /* o gerente pode excluir um grupo de um usuario onde este grupo esta em outra OU ? */
1349                                /* é o mesmo que o manager editar um grupo de outra OU */
1350                                $result['groups_info'][$i]['group_disabled'] = 'true';
1351                                foreach ($this->manager_contexts as $index=>$context)
1352                                {
1353                                        if (strpos(strtolower($entry[0]['dn']), strtolower($context)))
1354                                        {
1355                                                $result['groups_info'][$i]['group_disabled'] = 'false';
1356                                        }
1357                                }
1358
1359                                $i++;
1360                        }
1361                }
1362                return $result;
1363        }
1364
1365        function uidnumber2uid($uidnumber)
1366        {
1367                $justthese = array("uid");
1368                $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uidNumber=".$uidnumber."))";
1369                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1370                $entry = ldap_get_entries($this->ldap, $search);
1371                return $entry[0]['uid'][0];
1372        }
1373
1374        function uidnumber2mail($uidnumber)
1375        {
1376                $justthese = array("mail");
1377                $filter="(&(|(phpgwAccountType=u)(phpgwAccountType=l))(uidNumber=".$uidnumber."))";
1378                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1379                $entry = ldap_get_entries($this->ldap, $search);
1380                return $entry[0]['mail'][0];
1381        }
1382
1383       
1384        function change_user_context($dn, $newrdn, $newparent)
1385        {
1386                if (!ldap_rename ( $this->ldap, $dn, $newrdn, $newparent, true ))
1387                {
1388                        $return['status'] = false;
1389                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->change_user_context ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1390                }
1391                else
1392                        $return['status'] = true;
1393               
1394                return $return;
1395        }
1396       
1397        function replace_user_attributes($dn, $ldap_mod_replace)
1398        {
1399                if (!@ldap_mod_replace ( $this->ldap, $dn, $ldap_mod_replace ))
1400                {
1401                        $return['status'] = false;
1402                        $return['error_number'] = ldap_errno($this->ldap);
1403                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->replace_user_attributes ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1404                }
1405                else
1406                        $return['status'] = true;
1407               
1408                return $return;
1409        }
1410       
1411        function add_user_attributes($dn, $ldap_add)
1412        {
1413                if (!@ldap_mod_add ( $this->ldap, $dn, $ldap_add ))
1414                {
1415                        $return['status'] = false;
1416                        $return['error_number'] = ldap_errno($this->ldap);
1417                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_user_attributes ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1418                }
1419                else
1420                        $return['status'] = true;
1421               
1422                return $return;
1423        }
1424       
1425        function remove_user_attributes($dn, $ldap_remove)
1426        {
1427                if (!@ldap_mod_del ( $this->ldap, $dn, $ldap_remove ))
1428                {
1429                        $return['status'] = false;
1430                        $return['msg'] = $this->functions->lang('Error on function') . " ldap_functions->remove_user_attributes ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1431                }
1432                else
1433                        $return['status'] = true;
1434               
1435                return $return;
1436        }
1437       
1438        function set_user_password($uid, $password)
1439        {
1440                $justthese = array("userPassword");
1441                $filter="(&(phpgwAccountType=u)(uid=".$uid."))";
1442                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1443            $entry = ldap_get_entries($this->ldap, $search);
1444                $dn = $entry[0]['dn'];
1445                $userPassword = $entry[0]['userpassword'][0];
1446                $ldap_mod_replace['userPassword'] = $password;
1447                $this->replace_user_attributes($dn, $ldap_mod_replace);
1448                return $userPassword;
1449        }
1450       
1451        function delete_user($user_info)
1452        {
1453                // Verifica acesso do gerente (OU) ao tentar deletar um usuário.
1454                $manager_access = false;
1455                foreach ($this->manager_contexts as $index=>$context)
1456                {
1457                        if ( (strpos(strtolower($user_info['context']), strtolower($context))) || (strtolower($user_info['context']) == strtolower($context)) )
1458                        {
1459                                $manager_access = true;
1460                                break;
1461                        }
1462                }
1463                if (!$manager_access)
1464                {
1465                        $return['status'] = false;
1466                        $result['msg'] = $this->functions->lang('You do not have access to delete this user') . ".";
1467                        return $return;
1468                }
1469               
1470                $return['status'] = true;
1471                $return['msg'] = "";
1472                               
1473                // GROUPS
1474                $attrs = array();
1475                $attrs['memberuid'] = $user_info['uid'];
1476               
1477                if (count($user_info['groups_info']))
1478                {
1479                        foreach ($user_info['groups_info'] as $group_info)
1480                        {
1481                                $gidnumber = $group_info['gidnumber'];
1482                                $justthese = array("dn");
1483                                $filter="(&(phpgwAccountType=g)(gidnumber=".$gidnumber."))";
1484                                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1485                        $entry = ldap_get_entries($this->ldap, $search);
1486                                $dn = $entry[0]['dn'];
1487
1488                                if (!@ldap_mod_del($this->ldap, $dn, $attrs))
1489                                {
1490                                        $return['status'] = false;
1491                                        $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);
1492                                }
1493                        }
1494                }
1495               
1496                //INSTITUTIONAL ACCOUNTS
1497                $attrs = array();
1498                $attrs['mailForwardingAddress'] = $user_info['mail'];
1499               
1500                $justthese = array("dn");
1501                $filter="(&(phpgwAccountType=i)(mailforwardingaddress=".$user_info['mail']."))";
1502                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1503            $entries = ldap_get_entries($this->ldap, $search);
1504               
1505                for ($i=0; $i<$entries['count']; $i++)
1506                {
1507                        if ( !@ldap_mod_del($this->ldap, $entries[$i]['dn'], $attrs) )
1508                        {
1509                                $result['status'] = false;
1510                                $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);
1511                        }
1512                }
1513               
1514                // MAILLISTS
1515                $attrs = array();
1516                $attrs['mailForwardingAddress'] = $user_info['mail'];
1517               
1518                if (count($user_info['maillists_info']))
1519                {
1520                       
1521                        if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
1522                        {
1523                                $return['status'] = false;
1524                                $result['msg'] = $this->functions->lang('Connection with ldap_master fail') . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1525                                return $return;
1526                        }
1527                       
1528                        foreach ($user_info['maillists_info'] as $maillists_info)
1529                        {
1530                                $uid = $maillists_info['uid'];
1531                                $justthese = array("dn");
1532                                $filter="(&(phpgwAccountType=l)(uid=".$uid."))";
1533                                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1534                        $entry = ldap_get_entries($ldapMasterConnect, $search);
1535                                $dn = $entry[0]['dn'];
1536                       
1537                                if (!@ldap_mod_del($ldapMasterConnect, $dn, $attrs))
1538                                {
1539                                        $return['status'] = false;
1540                                        if (ldap_errno($ldapMasterConnect) == '50')
1541                                        {
1542                                                $result['msg'] =        $this->functions->lang('Error on the function') . ' ldap_functions->add_user2maillist' . ".\n" .
1543                                                                                        $this->functions->lang('The user used for record on LPDA, must have write access') . ".\n";
1544                                                                                        $this->functions->lang('The user') . ' ' . $_SESSION['phpgw_info']['expresso']['cc_ldap_server']['acc'] . ' ' . $this->functions->lang('does not have this access') . ".\n";
1545                                                                                        $this->functions->lang('Edit Global Catalog Config, in the admin module, and add an user with write access') . ".\n";
1546                                        }
1547                                        else
1548                                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_user, email lists ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
1549                                }
1550                        }
1551                        ldap_close($ldapMasterConnect);
1552                }
1553                       
1554                // UID
1555                $dn = "uid=" . $user_info['uid'] . "," . $user_info['context'];
1556                if (!@ldap_delete($this->ldap, $dn))
1557                {
1558                        $return['status'] = false;
1559                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_user, email lists ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($ldapMasterConnect);
1560                }
1561                /* jakjr */
1562                return $return;
1563        }
1564       
1565        function delete_maillist($uidnumber, $mail)
1566        {
1567                $return['status'] = true;
1568               
1569                $justthese = array("dn");
1570               
1571                // remove listas dentro de listas
1572                $filter="(&(phpgwAccountType=l)(mailForwardingAddress=".$mail."))";
1573                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1574                $entry = ldap_get_entries($this->ldap, $search);
1575                $attrs['mailForwardingAddress'] = $mail;
1576                for ($i=0; $i<=$entry['count']; $i++)
1577            {
1578                        $dn = $entry[$i]['dn'];
1579                @ldap_mod_del ( $this->ldap, $dn,  $attrs);
1580            }
1581               
1582                $filter="(&(phpgwAccountType=l)(uidnumber=".$uidnumber."))";
1583                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1584                $entry = ldap_get_entries($this->ldap, $search);
1585                $dn = $entry[0]['dn'];
1586               
1587                if (!@ldap_delete($this->ldap, $dn))
1588                {
1589                        $return['status'] = false;
1590                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_maillist ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1591                }
1592               
1593                return $return;
1594        }
1595
1596        function delete_group($gidnumber)
1597        {
1598                $return['status'] = true;
1599               
1600                $justthese = array("dn");
1601                $filter="(&(phpgwAccountType=g)(gidnumber=".$gidnumber."))";
1602                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1603                $entry = ldap_get_entries($this->ldap, $search);
1604                $dn = $entry[0]['dn'];
1605               
1606                if (!@ldap_delete($this->ldap, $dn))
1607                {
1608                        $return['status'] = false;
1609                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_group ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1610                }
1611               
1612                return $return;
1613        }
1614
1615        function check_access_to_renamed($uid)
1616        {
1617                $justthese = array("dn");
1618                $filter="(&(phpgwAccountType=u)(uid=$uid))";
1619               
1620                foreach ($this->manager_contexts as $index=>$context)
1621                {
1622                        $search = ldap_search($this->ldap, $context, $filter, $justthese);
1623                        $entry = ldap_get_entries($this->ldap, $search);
1624                        if ($entry['count'])
1625                                return true;
1626                }
1627            return false;
1628        }
1629
1630        function check_rename_new_uid($uid)
1631        {
1632                if ( !$ldapMasterConnect = $this->ldapMasterConnect() )
1633                        return false;
1634               
1635                $justthese = array("dn");
1636                $filter="(&(phpgwAccountType=u)(uid=$uid))";
1637               
1638                $search = ldap_search($ldapMasterConnect, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1639                $count_entries = @ldap_count_entries($ldapMasterConnect, $search);
1640               
1641                if ($count_entries)
1642                        return false;
1643                       
1644                return true;
1645        }
1646       
1647        function rename_uid($uid, $new_uid)
1648        {
1649                $return['status'] = true;
1650               
1651                $justthese = array("dn");
1652                $filter="(&(phpgwAccountType=u)(uid=".$uid."))";
1653                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1654            $entry = ldap_get_entries($this->ldap, $search);
1655                $dn = $entry[0]['dn'];
1656               
1657                $explode_dn = ldap_explode_dn($dn, 0);
1658                $rdn = "uid=" . $new_uid;
1659
1660                $parent = array();
1661                for ($j=1; $j<(count($explode_dn)-1); $j++)
1662                        $parent[] = $explode_dn[$j];
1663                $parent = implode(",", $parent);
1664               
1665                $return['new_dn'] = $rdn . ',' . $parent;
1666                       
1667                if (!@ldap_rename($this->ldap, $dn, $rdn, $parent, true))
1668                {
1669                        $return['status'] = false;
1670                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->rename_uid ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1671                }
1672               
1673                //Grupos
1674                $justthese = array("dn");
1675                $filter="(&(phpgwAccountType=g)(memberuid=".$uid."))";
1676                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1677            $entry = ldap_get_entries($this->ldap, $search);
1678        $array_mod_add['memberUid'] = $new_uid;
1679        $array_mod_del['memberUid'] = $uid;
1680
1681            for ($i=0; $i<=$entry['count']; $i++)
1682            {
1683                $dn = $entry[$i]['dn'];
1684                @ldap_mod_add ( $this->ldap, $dn,  $array_mod_add);
1685                @ldap_mod_del ( $this->ldap, $dn,  $array_mod_del);
1686            }
1687                return $return;
1688        }
1689
1690        function rename_cn($cn, $new_cn)
1691        {
1692                $return['status'] = true;
1693               
1694                $justthese = array("dn");
1695                $filter="(&(phpgwAccountType=g)(uid=".$cn."))";
1696                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1697            $entry = ldap_get_entries($this->ldap, $search);
1698                $dn = $entry[0]['dn'];
1699               
1700                $explode_dn = ldap_explode_dn($dn, 0);
1701                $rdn = "cn=" . $new_cn;
1702
1703                $parent = array();
1704                for ($j=1; $j<(count($explode_dn)-1); $j++)
1705                        $parent[] = $explode_dn[$j];
1706                $parent = implode(",", $parent);
1707               
1708                $return['new_dn'] = $rdn . ',' . $parent;
1709                       
1710                if (!@ldap_rename($this->ldap, $dn, $rdn, $parent, false))
1711                {
1712                        $return['status'] = false;
1713                }
1714               
1715                return $return;
1716        }
1717       
1718        function exist_sambadomains($contexts, $sambaDomainName)
1719        {
1720                $justthese = array("dn");
1721                $filter="(&(objectClass=sambaDomain)(sambaDomainName=$sambaDomainName))";
1722               
1723                foreach ($contexts as $index=>$context)
1724                {
1725                        $search = ldap_search($this->ldap, $context, $filter, $justthese);
1726                    $entry = ldap_get_entries($this->ldap, $search);
1727           
1728                        if ($entry['count'])
1729                                return true;
1730                }
1731                return false;
1732        }
1733       
1734        // Primeiro nilvel de organização.
1735        function exist_sambadomains_in_context($params)
1736        {
1737                $dn = $GLOBALS['phpgw_info']['server']['ldap_context'];
1738                $array_dn = ldap_explode_dn ( $dn, 0 );
1739               
1740                $context = $params['context'];
1741                $array_context = ldap_explode_dn ( $context, 0 );
1742               
1743                // Pego o setor no caso do contexto ser um sub-setor.
1744                if (($array_dn['count']+1) < ($array_context['count']))
1745                {
1746                        // inverto o array_dn para poder retirar o count
1747                        $array_dn_reverse  = array_reverse ( $array_dn, false );
1748                       
1749                        //retiro o count
1750                        array_pop($array_dn_reverse);
1751                       
1752                        //incluo o setor no dn
1753                        array_push ( $array_dn_reverse,  $array_context[ $array_context['count'] - 1 - $array_dn['count']]);
1754                       
1755                        // Volto a ordem natural
1756                        $array_dn  = array_reverse ( $array_dn_reverse, false );
1757                       
1758                        // Implodo
1759                        $context = implode ( ",", $array_dn );
1760                }
1761               
1762                $justthese = array("dn","sambaDomainName");
1763                $filter="(objectClass=sambaDomain)";
1764                $search = ldap_list($this->ldap, $context, $filter, $justthese);
1765            $entry = ldap_get_entries($this->ldap, $search);
1766           
1767            for ($i=0; $i<$entry['count']; $i++)
1768            {
1769                        $return['sambaDomains'][$i] = $entry[$i]['sambadomainname'][0];
1770            }
1771           
1772                if ($entry['count'])
1773                        $return['status'] = true;
1774                else
1775                        $return['status'] = false;
1776                       
1777                return $return;
1778        }
1779        function exist_domain_name_sid($sambadomainname, $sambasid)
1780        {
1781                $context = $GLOBALS['phpgw_info']['server']['ldap_context'];
1782
1783                $justthese = array("dn","sambaDomainName");
1784                $filter="(&(objectClass=sambaDomain)(sambaSID=$sambasid)(sambaDomainName=$sambadomainname))";
1785                $search = ldap_search($this->ldap, $context, $filter, $justthese);
1786            $count_entries = ldap_count_entries($this->ldap, $search);
1787           
1788            if ($count_entries > 0)
1789                return true;
1790            else
1791                return false;
1792        }
1793       
1794        function add_sambadomain($sambadomainname, $sambasid, $context)
1795        {
1796                $result = array();
1797               
1798                $dn                                                             = "sambaDomainName=$sambadomainname,$context";
1799                $entry['sambaSID']                                      = $sambasid;
1800                $entry['objectClass']                           = 'sambaDomain';
1801                $entry['sambaAlgorithmicRidBase']       = '1000';
1802                $entry['sambaDomainName']                       = $sambadomainname;
1803               
1804                if (!@ldap_add ( $this->ldap, $dn, $entry ))
1805                {
1806                        $return['status'] = false;
1807                        $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->add_sambadomain ($dn)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1808                }
1809                else
1810                        $return['status'] = true;
1811               
1812                return $return;
1813        }
1814       
1815        function delete_sambadomain($sambadomainname)
1816        {
1817                $return['status'] = true;
1818                $filter="(sambaDomainName=$sambadomainname)";
1819                $search = ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter);
1820            $entry = ldap_get_entries($this->ldap, $search);
1821         
1822                if ($entry['count'] != 0)
1823            {
1824                        $dn = $entry[0]['dn'];
1825                       
1826                        if (!@ldap_delete($this->ldap, $dn))
1827                        {
1828                                $return['status'] = false;
1829                                $result['msg'] = $this->functions->lang('Error on function') . " ldap_functions->delete_sambadomain ($sambadomainname)" . ".\n" . $this->functions->lang('Server returns') . ': ' . ldap_error($this->ldap);
1830                        }
1831            }
1832           
1833                return $return;
1834        }
1835       
1836        function search_user($params)
1837        {
1838                $search = $params['search'];
1839                $justthese = array("cn","uid", "mail");
1840        $users_list=ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], "(&(phpgwAccountType=u) (|(cn=*$search*)(mail=$search*)) )", $justthese);
1841       
1842        if (ldap_count_entries($this->ldap, $users_list) == 0)
1843        {
1844                $return['status'] = 'false';
1845                $result['msg'] = $this->functions->lang('Any result was found') . '.';
1846                return $return;
1847        }
1848       
1849        ldap_sort($this->ldap, $users_list, "cn");
1850       
1851        $entries = ldap_get_entries($this->ldap, $users_list);
1852               
1853                $options = '';
1854                for ($i=0; $i<$entries['count']; $i++)
1855                {
1856                        $options .= "<option value=" . $entries[$i]['uid'][0] . ">" . $entries[$i]['cn'][0] . " (".$entries[$i]['mail'][0].")" . "</option>";
1857                }
1858       
1859        return $options;               
1860        }
1861       
1862        function create_institutional_accounts($params)
1863        {
1864                /* Begin: Access verification */
1865                if (!$this->functions->check_acl($_SESSION['phpgw_info']['expresso']['user']['account_lid'], 'add_institutional_accounts'))
1866                {
1867                        $return['status'] = false;
1868                        $return['msg'] = $this->functions->lang('You do not have right to create institutional accounts') . ".";
1869                        return $return;
1870                }
1871               
1872                $access_granted = false;
1873                foreach ($this->manager_contexts as $idx=>$manager_context)
1874                {
1875                        if (stristr($params['context'], $manager_context))
1876                        {
1877                                $access_granted = true;
1878                                break;
1879                        }
1880                }
1881                if (!$access_granted)
1882                {
1883                        $return['status'] = false;
1884                        $return['msg'] = $this->functions->lang('You do not have access to this organization') . ".";
1885                        return $return;
1886                }
1887                /* End: Access verification */
1888
1889                /* Begin: Validation */
1890                if ( (empty($params['cn'])) || (empty($params['mail'])) )
1891                {
1892                        $result['status'] = false;
1893                        $result['msg']  = $this->functions->lang('Field mail or name is empty');
1894                        return $result;
1895                }
1896
1897                if (! eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$", $params['mail']) )
1898                {
1899                        $result['status'] = false;
1900                        $result['msg']  = $this->functions->lang('Field mail is not formed correcty') . '.';
1901                        return $result;
1902                }
1903
1904                $uid = 'institutional_account_' . $params['mail'];
1905                $dn = "uid=$uid," . $params['context'];
1906
1907                $filter = "(mail=".$params['mail'].")";
1908                $justthese = array("cn");
1909                $search = @ldap_search($this->ldap, $GLOBALS['phpgw_info']['server']['ldap_context'], $filter, $justthese);
1910                $entries = @ldap_get_entries($this->ldap,$search);
1911                if ($entries['count'] != 0)
1912                {
1913                        $result['status'] = false;
1914                        $result['msg'] = $this->functions->lang('Field mail already in use');
1915                        return $result;
1916                }
1917                /* End: Validation */
1918                                               
1919                $info = array();
1920                $info['cn']                                     = $params['cn'];
1921                $info['sn']                                     = $params['cn'];
1922                $info['uid']                            = $uid;
1923                $info['mail']                           = $params['mail'];
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.