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

Revision 1867, 77.5 KB checked in by valmir.sena, 14 years ago (diff)

Ticket #835 - Permitir ou não o uso duplicado de CPF

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