source: branches/2.2/expressoAdmin1_2/inc/class.user.inc.php @ 3528

Revision 3528, 46.6 KB checked in by alexandrecorreia, 13 years ago (diff)

Ticket #1415 - Correcao para a deteccao da versao do cyrus, maior que 22

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2        /**********************************************************************************\
3        * Expresso Administração                                                                                              *
4        * by Joao Alfredo Knopik Junior (joao.alfredo@gmail.com, jakjr@celepar.pr.gov.br) *
5        * --------------------------------------------------------------------------------*
6        *  This program is free software; you can redistribute it and/or modify it                *
7        *  under the terms of the GNU General Public License as published by the                  *
8        *  Free Software Foundation; either version 2 of the License, or (at your                 *
9        *  option) any later version.                                                                                                     *
10        \**********************************************************************************/
11       
12        include_once('class.ldap_functions.inc.php');
13        include_once('class.db_functions.inc.php');
14        include_once('class.imap_functions.inc.php');
15        include_once('class.functions.inc.php');
16       
17        class user
18        {
19                var $ldap_functions;
20                var $db_functions;
21                var $imap_functions;
22                var $functions;
23                var $current_config;
24               
25                function user()
26                {
27                        $this->ldap_functions = new ldap_functions;
28                        $this->db_functions = new db_functions;
29                        $this->imap_functions = new imap_functions;
30                        $this->functions = new functions;
31                        $this->current_config = $_SESSION['phpgw_info']['expresso']['expressoAdmin'];
32                }
33               
34                function create($params)
35                {
36                        $return['status'] = true;
37               
38                        if($this->db_functions->use_cota_control()) {
39                                //Verifica quota de usuários e disco           
40                                $setor = $this->functions->get_info($params['context']);
41                                if (!$this->functions->existe_quota_usuario($setor[0])) {
42                                        $return['status'] = false;
43                                        $return['msg'] = $this->functions->lang("user cota exceeded");//TODO colocar valor de acordo com tabela de traduções.
44                                        return $return;
45                                }
46                                if (!$this->functions->existe_quota_disco($setor[0],$params['mailquota'])) {
47                                        $return['status'] = false;
48                                        $return['msg'] = $this->functions->lang("disk cota exceeded");//TODO colocar valor de acordo com tabela de traduções.
49                                        return $return;                         
50                                }
51                        }
52               
53                        // Verifica o acesso do gerente
54                        if ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'add_users'))
55                        {
56                                // Adiciona a organização na frente do uid.
57                                if ($this->current_config['expressoAdmin_prefix_org'] == 'true')
58                                {
59                                        $context_dn = ldap_explode_dn(strtolower($GLOBALS['phpgw_info']['server']['ldap_context']), 1);
60                               
61                                        $explode_dn = ldap_explode_dn(strtolower($params['context']), 1);
62                                        $explode_dn = array_reverse($explode_dn);
63                                        //$params['uid'] = $explode_dn[3] . '-' . $params['uid'];
64                                        $params['uid'] = $explode_dn[$context_dn['count']] . '-' . $params['uid'];
65                                }
66                       
67                                // Leio o ID a ser usado na criação do objecto. Esta função já incrementa o ID no BD.
68                                $next_id = ($this->db_functions->get_next_id('accounts'));
69                                if ((!is_numeric($next_id['id'])) || (!$next_id['status']))
70                                {
71                                        $return['status'] = false;
72                                        $return['msg'] = $this->functions->lang('problems getting user id') . ".\n" . $id['msg'];
73                                        return $return;
74                                }
75                                else
76                                {
77                                        $id = $next_id['id'];
78                                }
79                       
80                                // Cria array para incluir no LDAP
81                                $dn = 'uid=' . $params['uid'] . ',' . $params['context'];               
82                       
83                                $user_info = array();
84                                $user_info['accountStatus']                     = $params['accountstatus'] == 1 ? 'active' : 'desactive';
85                                $user_info['cn']                                                = $params['givenname'] . ' ' . $params['sn'];
86                                $user_info['gidNumber']                                 = $params['gidnumber'];
87                                $user_info['givenName']                                 = $params['givenname'];
88                                $user_info['homeDirectory']                             = '/home/' . $params['uid'];
89                                $user_info['mail']                                              = $params['mail'];
90                                $user_info['objectClass'][]                             = 'posixAccount';
91                                $user_info['objectClass'][]                             = 'inetOrgPerson';
92                                $user_info['objectClass'][]                             = 'shadowAccount';
93                                $user_info['objectClass'][]                             = 'qmailuser';
94                                $user_info['objectClass'][]                             = 'phpgwAccount';
95                                $user_info['objectClass'][]                             = 'top';
96                                $user_info['objectClass'][]                             = 'person';
97                                $user_info['objectClass'][]                             = 'organizationalPerson';
98                                $user_info['phpgwAccountExpires']               = '-1';
99                                $user_info['phpgwAccountType']                  = 'u';
100                                $user_info['sn']                                                = $params['sn'];
101                                $user_info['uid']                                               = $params['uid'];
102                                $user_info['uidnumber']                                 = $id;
103                                $user_info['userPassword']                              = '{md5}' . base64_encode(pack("H*",md5($params['password1'])));
104                               
105                                if ($params['passwd_expired'] == '1')
106                                        $user_info['phpgwLastPasswdChange'] = '0';
107                               
108                                // Gerenciar senhas RFC2617
109                                if ($this->current_config['expressoAdmin_userPasswordRFC2617'] == 'true')
110                                {
111                                        $realm          = $this->current_config['expressoAdmin_realm_userPasswordRFC2617'];
112                                        $uid            = $user_info['uid'];
113                                        $password       = $params['password1'];
114                                        $user_info['userPasswordRFC2617'] = $realm . ':      ' . md5("$uid:$realm:$password");
115                                }
116                               
117                                if ($params['phpgwaccountstatus'] == '1')
118                                        $user_info['phpgwAccountStatus'] = 'A';
119                       
120                                if ($params['departmentnumber'] != '')
121                                        $user_info['departmentnumber']  = $params['departmentnumber'];
122                       
123                                if ($params['telephonenumber'] != '')
124                                        $user_info['telephoneNumber']   = $params['telephonenumber'];
125                                               
126                                // Cria user_info no caso de ter alias e forwarding email.
127                                foreach ($params['mailalternateaddress'] as $index=>$mailalternateaddress)
128                                {
129                                        if ($mailalternateaddress != '')
130                                                $user_info['mailAlternateAddress'][] = $mailalternateaddress;
131                                }
132                       
133                                foreach ($params['mailforwardingaddress'] as $index=>$mailforwardingaddress)
134                                {
135                                        if ($mailforwardingaddress != '')
136                                                $user_info['mailForwardingAddress'][] = $mailforwardingaddress;
137                                }
138                               
139                                if ($params['deliverymode'])
140                                        $user_info['deliveryMode'] = 'forwardOnly';
141                       
142                                //Ocultar da pesquisa e do catálogo
143                                if ($params['phpgwaccountvisible'])
144                                        $user_info['phpgwAccountVisible'] = '-1';
145
146                                // Suporte ao SAMBA
147                                if (($this->current_config['expressoAdmin_samba_support'] == 'true') && ($params['use_attrs_samba'] == 'on'))
148                                {
149                                       
150                                        // Qualquer um que crie um usuário, deve ter permissão para adicionar a senha samba.
151                                        // Verifica o acesso do gerente aos atributos samba
152                                        //if ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_sambausers_attributes'))
153                                        //{
154                                                //Verifica se o binario para criar as senhas do samba exite.
155                                                if (!is_file('/home/expressolivre/mkntpwd'))
156                                                {
157                                                        $return['status'] = false;
158                                                        $return['msg'] .=
159                                                                        $this->functions->lang("the binary file /home/expressolivre/mkntpwd does not exist") . ".\\n" .
160                                                                        $this->functions->lang("it is needed to create samba passwords") . ".\\n" .
161                                                                        $this->functions->lang("alert your administrator about this") . ".";
162                                                }
163                                                else
164                                                {
165                                                        $user_info['objectClass'][]             = 'sambaSamAccount';
166                                                        $user_info['loginShell']                        = '/bin/bash';
167       
168                                                        $user_info['sambaSID']                          = $params['sambadomain'] . '-' . ((2 * $id)+1000);
169                                                        $user_info['sambaPrimaryGroupSID']      = $params['sambadomain'] . '-' . ((2 * $user_info['gidNumber'])+1001);
170
171                                                        $user_info['sambaAcctFlags']            = $params['sambaacctflags'];
172                       
173                                                        $user_info['sambaLogonScript']          = $params['sambalogonscript'];
174                                                        $user_info['homeDirectory']                     = $params['sambahomedirectory'];
175                       
176                                                        $user_info['sambaLMPassword']           = exec('/home/expressolivre/mkntpwd -L "'.$params['password1'] . '"');
177                                                        $user_info['sambaNTPassword']           = exec('/home/expressolivre/mkntpwd -N "'.$params['password1'] . '"');
178                                                       
179                                                        $user_info['sambaPasswordHistory']      = '0000000000000000000000000000000000000000000000000000000000000000';
180                       
181                                                        $user_info['sambaPwdCanChange']         = strtotime("now");
182                                                        $user_info['sambaPwdLastSet']           = strtotime("now");
183                                                        $user_info['sambaPwdMustChange']        = '2147483647';
184                                                }
185                                        //}
186                                }
187                               
188                                // Verifica o acesso do gerente aos atributos corporativos
189                                if ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'manipulate_corporative_information'))
190                                {
191                                        //retira caracteres que não são números.
192                                        $params['corporative_information_cpf'] = ereg_replace("[^0-9]", "", $params['corporative_information_cpf']);
193                                        //description
194                                        $params['corporative_information_description'] = utf8_encode($params['corporative_information_description']);
195                                        foreach ($params as $atribute=>$value)
196                                        {
197                                                $pos = strstr($atribute, 'corporative_information_');
198                                                if ($pos !== false)
199                                                {
200                                                        if ($params[$atribute])
201                                                        {
202                                                                $ldap_atribute = str_replace("corporative_information_", "", $atribute);
203                                                                $user_info[$ldap_atribute] = $params[$atribute];
204                                                        }
205                                                }
206                                        }
207                                }
208                               
209                                $result = $this->ldap_functions->ldap_add_entry($dn, $user_info);
210                                if (!$result['status'])
211                                {
212                                        $return['status'] = false;
213                                        $return['msg'] .= $result['msg'];
214                                }
215                       
216                                // Chama funcao para salvar foto no OpenLDAP.                   
217                                if ( ($_FILES['photo']['name'] != '') && ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_users_picture')) )
218                                {
219                                        $result = $this->ldap_functions->ldap_save_photo($dn, $_FILES['photo']['tmp_name']);
220                                        if (!$result['status'])
221                                        {
222                                                $return['status'] = false;
223                                                $return['msg'] .= $result['msg'];
224                                        }
225                                }
226                       
227                                //GROUPS
228                                if ($params['groups'])
229                                {
230                                        foreach ($params['groups'] as $gidnumber)
231                                        {
232                                                $result = $this->ldap_functions->add_user2group($gidnumber, $user_info['uid']);
233                                                if (!$result['status'])
234                                                {
235                                                        $return['status'] = false;
236                                                        $return['msg'] .= $result['msg'];
237                                                }
238                                                $result = $this->db_functions->add_user2group($gidnumber, $id);
239                                                if (!$result['status'])
240                                                {
241                                                        $return['status'] = false;
242                                                        $return['msg'] .= $result['msg'];
243                                                }
244                                        }
245                                }
246                       
247                                // Inclusao do Mail do usuário nas listas de email selecionadas.
248                                if ($params['maillists'])
249                                {
250                                        foreach($params['maillists'] as $uid)
251                        {
252                                                $result = $this->ldap_functions->add_user2maillist($uid, $user_info['mail']);
253                                                if (!$result['status'])
254                                                {
255                                                        $return['status'] = false;
256                                                        $return['msg'] .= $result['msg'];
257                                                }
258                        }
259                                }
260                       
261                                // APPS
262                                if (count($params['apps']))
263                                {
264                                        $result = $this->db_functions->add_id2apps($id, $params['apps']);
265                                        if (!$result['status'])
266                                        {
267                                                $return['status'] = false;
268                                                $return['msg'] .= $result['msg'];
269                                        }
270                                }
271
272                                // Chama funcao para incluir no pgsql as preferencia de alterar senha.
273                                if ($params['changepassword'])
274                                {
275                                        $result = $this->db_functions->add_pref_changepassword($id);
276                                        if (!$result['status'])
277                                        {
278                                                $return['status'] = false;
279                                                $return['msg'] .= $result['msg'];
280                                        }
281                                }                                       
282                                                       
283                                // Chama funcao para criar mailbox do usuario, no imap-cyrus.
284                                $result = $this->imap_functions->create($params['uid'], $params['mailquota']);
285                                if (!$result['status'])
286                                {
287                                        $return['status'] = false;
288                                $return['msg'] .= $result['msg'];
289                                }
290
291                                $this->db_functions->write_log("created user",$dn);
292                        }
293
294                        return $return;
295                }
296               
297                function save($new_values)
298                {
299                        $return['status'] = true;
300                       
301                        $old_values = $this->get_user_info($new_values['uidnumber']);
302                       
303                        $dn = 'uid=' . $old_values['uid'] . ',' . strtolower($old_values['context']);
304
305                        //retira caracteres que não são números.
306                        $new_values['corporative_information_cpf'] = ereg_replace("[^0-9]", "", $new_values['corporative_information_cpf']);
307
308                        $diff = array_diff($new_values, $old_values);
309                       
310                        /*
311                        echo '<pre>';
312                        echo '--- OLD: ';
313                        print_r($old_values);
314                        echo '<br>--- NEW: ';
315                        print_r($new_values);
316                        echo '<br>';
317                        exit;
318                        */
319
320                        //Verifica quota de disco, como estou alterando, não preciso checar quota de usuários.
321                        if($this->db_functions->use_cota_control()) {           
322                                $setor = $this->functions->get_info($new_values['context']);
323                                if (!$this->functions->existe_quota_disco($setor[0],$new_values['mailquota'])) {
324                                        $return['status'] = false;
325                                        $return['msg'] = "Quota em disco excedida...";//TODO colocar valor de acordo com tabela de traduções.
326                                        return $return;                         
327                                }
328                        }
329
330                        $manager_account_lid = $_SESSION['phpgw_session']['session_lid'];
331                        if ((!$this->functions->check_acl($manager_account_lid,'edit_users')) &&
332                                (!$this->functions->check_acl($manager_account_lid,'change_users_password')) &&
333                                (!$this->functions->check_acl($manager_account_lid,'edit_sambausers_attributes')) &&
334                                (!$this->functions->check_acl($manager_account_lid,'manipulate_corporative_information')) &&
335                                (!$this->functions->check_acl($manager_account_lid,'edit_users_phonenumber'))
336                                )
337                        {
338                                $return['status'] = false;
339                                $return['msg'] = $this->functions->lang('You do not have access to edit user informations') . '.';
340                                return $return;
341                        }
342
343                        // Check manager access
344                        if ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_users'))
345                        {
346                                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
347                                // Change user organization
348                                if ($diff['context'])
349                                {
350                                        if (strcasecmp($old_values['context'], $new_values['context']) != 0)
351                                        {
352                                                $newrdn = 'uid=' . $new_values['uid'];
353                                                $newparent = $new_values['context'];
354                                                $result =  $this->ldap_functions->change_user_context($dn, $newrdn, $newparent);
355                                                if (!$result['status'])
356                                                {
357                                                        $return['status'] = false;
358                                                        $return['msg'] .= $result['msg'];
359                                                }
360                                                else
361                                                {
362                                                        $dn = $newrdn . ',' . $newparent;
363                                                        $this->db_functions->write_log('modified user context', $dn . ': ' . $old_values['uid'] . '->' . $new_values['context']);
364                                                }
365                                        }
366                                }
367                       
368                                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
369                                // REPLACE some attributes
370                                if ($diff['givenname'])
371                                {
372                                        $ldap_mod_replace['givenname'] = $new_values['givenname'];
373                                        $ldap_mod_replace['cn'] = $new_values['givenname'] . ' ' . $new_values['sn'];
374                                        $this->db_functions->write_log("modified first name", "$dn: " . $old_values['givenname'] . "->" . $new_values['givenname']);
375                                }
376                                if ($diff['sn'])
377                                {
378                                        $ldap_mod_replace['sn'] = $new_values['sn'];
379                                        $ldap_mod_replace['cn'] = $new_values['givenname'] . ' ' . $new_values['sn'];
380                                        $this->db_functions->write_log("modified last name", "$dn: " . $old_values['sn'] . "->" . $new_values['sn']);
381                                }
382                                if ($diff['mail'])
383                                {
384                                        $ldap_mod_replace['mail'] = $new_values['mail'];
385                                        $this->ldap_functions->replace_user2maillists($new_values['mail'], $old_values['mail']);
386                                        $this->ldap_functions->replace_mail_from_institutional_account($new_values['mail'], $old_values['mail']);
387                                        $this->db_functions->write_log("modified user email", "$dn: " . $old_values['mail'] . "->" . $new_values['mail']);
388                                }
389                                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
390                                // Passwd Expired - Com atributo
391                                if (($old_values['passwd_expired'] != 0) && ($new_values['passwd_expired'] == '1'))
392                                {
393                                        $ldap_mod_replace['phpgwlastpasswdchange'] = '0';
394                                        $this->db_functions->write_log("Expired user password","$dn");
395                                }
396                        }
397                       
398                        if ( ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_users')) ||
399                             ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'change_users_password')) )
400                        {
401                                if ($diff['password1'])
402                                {
403                                        $ldap_mod_replace['userPassword'] = '{md5}' . base64_encode(pack("H*",md5($new_values['password1'])));
404                                       
405                                        // Suporte ao SAMBA
406                                        if (($this->current_config['expressoAdmin_samba_support'] == 'true') && ($new_values['userSamba']) && ($new_values['use_attrs_samba'] == 'on'))
407                                        {
408                                                $ldap_mod_replace['sambaLMPassword'] = exec('/home/expressolivre/mkntpwd -L "'.$new_values['password1'] . '"');
409                                                $ldap_mod_replace['sambaNTPassword'] = exec('/home/expressolivre/mkntpwd -N "'.$new_values['password1'] . '"');
410                                        }
411                                       
412                                        // Gerenciar senhas RFC2617
413                                        if ($this->current_config['expressoAdmin_userPasswordRFC2617'] == 'true')
414                                        {
415                                                $realm          = $this->current_config['expressoAdmin_realm_userPasswordRFC2617'];
416                                                $uid            = $new_values['uid'];
417                                                $password       = $new_values['password1'];
418                                                $passUserRFC2617 = $realm . ':      ' . md5("$uid:$realm:$password");
419                                               
420                                                if ($old_values['userPasswordRFC2617'] != '')
421                                                        $ldap_mod_replace['userPasswordRFC2617'] = $passUserRFC2617;
422                                                else
423                                                        $ldap_add['userPasswordRFC2617'] = $passUserRFC2617;
424                                        }
425                                       
426                                        $this->db_functions->write_log("modified user password",$dn);
427                                }
428                        }
429
430                        if ( ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_users')) ||
431                             ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_users_phonenumber')) )
432                        {
433                                if (($diff['telephonenumber']) && ($old_values['telephonenumber'] != ''))
434                                {
435                                        $ldap_mod_replace['telephonenumber'] = $new_values['telephonenumber'];
436                                        $this->db_functions->write_log('modified user telephonenumber', $dn . ': ' . $old_values['telephonenumber'] . '->' . $new_values['telephonenumber']);
437                                }
438                                else if (($old_values['telephonenumber'] != '') && ($new_values['telephonenumber'] == ''))
439                                {
440                                        $ldap_remove['telephonenumber'] = array();
441                                        $this->db_functions->write_log("removed user phone",$dn);
442                                }
443                                else if (($old_values['telephonenumber'] == '') && ($new_values['telephonenumber'] != ''))
444                                {
445                                        $ldap_add['telephonenumber'] = $new_values['telephonenumber'];
446                                        $this->db_functions->write_log("added user phone",$dn);
447                                }
448                        }
449                       
450                        // REPLACE, ADD & REMOVE COPORATIVEs ATRIBUTES
451                        // Verifica o acesso do gerente aos atributos corporativos
452                       
453                        if ( ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_users')) ||
454                             ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'manipulate_corporative_information')) )
455                        {
456                                foreach ($new_values as $atribute=>$value)
457                                {
458                                        $pos = strstr($atribute, 'corporative_information_');
459                                        if ($pos !== false)
460                                        {
461                                                $ldap_atribute = str_replace("corporative_information_", "", $atribute);
462                                                // REPLACE CORPORATIVE ATTRIBUTES
463                                                if (($diff[$atribute]) && ($old_values[$atribute] != ''))
464                                                {
465                                                        $ldap_atribute = str_replace("corporative_information_", "", $atribute);
466                                                        $ldap_mod_replace[$ldap_atribute] = utf8_encode($new_values[$atribute]);
467                                                        $this->db_functions->write_log('modified user attribute', $dn . ': ' . $ldap_atribute . ': ' . $old_values[$atribute] . '->' . $new_values[$atribute]);
468                                                }
469                                                //ADD CORPORATIVE ATTRIBUTES
470                                                elseif (($old_values[$atribute] == '') && ($new_values[$atribute] != ''))
471                                                {
472                                                        $ldap_add[$ldap_atribute] = utf8_encode($new_values[$atribute]);
473                                                        $this->db_functions->write_log('added user attribute', $dn . ': ' . $ldap_atribute . ': ' . $old_values[$atribute] . '->' . $new_values[$atribute]);
474                                                }
475                                                //REMOVE CORPORATIVE ATTRIBUTES
476                                                elseif (($old_values[$atribute] != '') && ($new_values[$atribute] == ''))
477                                                {
478                                                        $ldap_remove[$ldap_atribute] = array();
479                                                        $this->db_functions->write_log('removed user attribute', $dn . ': ' . $ldap_atribute . ': ' . $old_values[$atribute] . '->' . $new_values[$atribute]); 
480                                                }
481                                        }
482                                }
483                        }
484                       
485                        //Suporte ao SAMBA
486                        if ( ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_users')) ||
487                             ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_sambausers_attributes')) )
488                        {
489                               
490                                if ($diff['gidnumber'])
491                                {
492                                        $ldap_mod_replace['gidnumber'] = $new_values['gidnumber'];
493                                        $this->db_functions->write_log('modified user primary group', $dn . ': ' . $old_values['gidnumber'] . '->' . $new_values['gidnumber']);
494                                }
495                               
496                                if (($this->current_config['expressoAdmin_samba_support'] == 'true') && ($new_values['userSamba']) && ($new_values['use_attrs_samba'] == 'on'))
497                                {
498                                        if ($diff['gidnumber'])
499                                        {
500                                                $ldap_mod_replace['sambaPrimaryGroupSID']       = $this->current_config['expressoAdmin_sambaSID'] . '-' . ((2 * $new_values['gidnumber'])+1001);
501                                                $this->db_functions->write_log('modified user sambaPrimaryGroupSID', $dn);
502                                        }
503                                       
504                                        if ($diff['sambaacctflags'])
505                                        {
506                                                $ldap_mod_replace['sambaacctflags'] = $new_values['sambaacctflags'];
507                                                $this->db_functions->write_log("modified user sambaacctflags",$dn);
508                                        }
509                                        if ($diff['sambalogonscript'])
510                                        {
511                                                $ldap_mod_replace['sambalogonscript'] = $new_values['sambalogonscript'];
512                                                $this->db_functions->write_log("modified user sambalogonscript",$dn);
513                                        }
514                                        if ($diff['sambahomedirectory'])
515                                        {
516                                                $ldap_mod_replace['homedirectory'] = $new_values['sambahomedirectory'];
517                                                $this->db_functions->write_log("modified user homedirectory",$dn);
518                                        }
519                                        if ($diff['sambadomain'])
520                                        {
521                                                $ldap_mod_replace['sambaSID']                           = $diff['sambadomain'] . '-' . ((2 * $old_values['uidnumber'])+1000);
522                                                $ldap_mod_replace['sambaPrimaryGroupSID']       = $diff['sambadomain'] . '-' . ((2 * $old_values['gidnumber'])+1001);
523                                                $this->db_functions->write_log('modified user samba domain', $dn . ': ' . $old_values['sambadomain'] . '->' . $new_values['sambadomain']);
524                                        }
525                                }
526                        }
527                       
528                        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
529                        // ADD or REMOVE some attributes
530                        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
531
532                        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
533                        // PHOTO
534                        if ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_users_picture'))
535                        {
536                                if ($new_values['delete_photo'])
537                                {
538                                        $this->ldap_functions->ldap_remove_photo($dn);
539                                        $this->db_functions->write_log("removed user photo",$dn);
540                                }
541                                elseif ($_FILES['photo']['name'] != '')
542                                {
543                                       
544                                        $size_conf = $this->current_config['expressoAdmin_photo_length'] == '' ? 10000:$this->current_config['expressoAdmin_photo_length'] ;
545                                        if ($_FILES['photo']['size'] > $size_conf)
546                                        {
547                                                $return['status'] = false;
548                                                $return['msg'] .= $this->functions->lang('User photo could not be save because is bigger the').' '.($size_conf/1024).' kb.';
549                                        }
550                                        else
551                                        {
552                                                if ($new_values['photo_exist'])
553                                                {
554                                                        $photo_exist = true;
555                                                        $this->db_functions->write_log("mofified user photo",$dn);
556                                                }
557                                                else
558                                                {
559                                                        $photo_exist = false;
560                                                        $this->db_functions->write_log("added user photo",$dn);
561                                                }                               
562                                                $this->ldap_functions->ldap_save_photo($dn, $_FILES['photo']['tmp_name'], $new_values['photo_exist'], $photo_exist);
563                                        }
564                                }       
565                        }
566                       
567                        // Verifica o acesso ára adicionar ou remover tais atributos
568                        if ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_users'))
569                        {
570                                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
571                                // Passwd Expired - Sem atributo
572                                if (($old_values['passwd_expired'] == '') && ($new_values['passwd_expired'] == '1'))
573                                {
574                                        $ldap_add['phpgwlastpasswdchange'] = '0';
575                                        $this->db_functions->write_log("expired user password",$dn);
576                                }
577                                if (($old_values['passwd_expired'] == '0') && ($new_values['passwd_expired'] == ''))
578                                {
579                                        $ldap_remove['phpgwlastpasswdchange'] = array();
580                                        $this->db_functions->write_log("removed expiry from user password",$dn);
581                                }
582                                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
583                                // PREF_CHANGEPASSWORD
584                                if (($old_values['changepassword'] == '') && ($new_values['changepassword'] != ''))
585                                {
586                                        $this->db_functions->add_pref_changepassword($new_values['uidnumber']);
587                                        $this->db_functions->write_log("turn on changepassword",$dn);
588                                }
589                                if (($old_values['changepassword'] != '') && ($new_values['changepassword'] == ''))
590                                {
591                                        $this->db_functions->remove_pref_changepassword($new_values['uidnumber']);
592                                        $this->db_functions->write_log("turn of changepassword",$dn);
593                                }
594                                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
595                                // ACCOUNT STATUS
596                                if (($old_values['phpgwaccountstatus'] == '') && ($new_values['phpgwaccountstatus'] != ''))
597                                {
598                                        $ldap_add['phpgwaccountstatus'] = 'A';
599                                        $this->db_functions->write_log("turn on user account",$dn);
600                                }
601                                if (($old_values['phpgwaccountstatus'] != '') && ($new_values['phpgwaccountstatus'] == ''))
602                                {
603                                        $ldap_remove['phpgwaccountstatus'] = array();
604                                        $this->db_functions->write_log("turn off user account",$dn);
605                                }
606
607                                if ($new_values['phpgwaccountexpired'] == '1') /////////////////////////
608                                {
609                                        $this->db_functions->write_log("Reactivated blocked user by downtime",'',$dn,'','');
610                                        $this->db_functions->reactivate_inactive_user($old_values['uidnumber']);
611                                }
612                                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
613                                // ACCOUNT VISIBLE
614                                if (($old_values['phpgwaccountvisible'] == '') && ($new_values['phpgwaccountvisible'] != ''))
615                                {
616                                        $ldap_add['phpgwaccountvisible'] = '-1';
617                                        $this->db_functions->write_log("turn on phpgwaccountvisible",$dn);
618                                }
619                                if (($old_values['phpgwaccountvisible'] != '') && ($new_values['phpgwaccountvisible'] == ''))
620                                {
621                                        $ldap_remove['phpgwaccountvisible'] = array();
622                                        $this->db_functions->write_log("turn off phpgwaccountvisible",$dn);
623                                }
624                                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
625                                // Mail Account STATUS
626                                if (($old_values['accountstatus'] == '') && ($new_values['accountstatus'] != ''))
627                                {
628                                        $ldap_add['accountstatus'] = 'active';
629                                        $this->db_functions->write_log("turn on user account email",$dn);
630                                }
631                                if (($old_values['accountstatus'] != '') && ($new_values['accountstatus'] == ''))
632                                {
633                                        $ldap_remove['accountstatus'] = array();
634                                        $this->db_functions->write_log("turn off user account email",$dn);
635                                }
636                                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
637                                // MAILALTERNATEADDRESS
638                                if (!$new_values['mailalternateaddress'])
639                                        $new_values['mailalternateaddress'] = array();
640                                if (!$old_values['mailalternateaddress'])
641                                        $old_values['mailalternateaddress'] = array();
642                                $add_mailalternateaddress = array_diff($new_values['mailalternateaddress'], $old_values['mailalternateaddress']);
643                                $remove_mailalternateaddress = array_diff($old_values['mailalternateaddress'], $new_values['mailalternateaddress']);
644                                foreach ($add_mailalternateaddress as $index=>$mailalternateaddress)
645                                {
646                                        if ($mailalternateaddress != '')
647                                        {
648                                                $ldap_add['mailalternateaddress'][] = $mailalternateaddress;
649                                                $this->db_functions->write_log("added mailalternateaddress","$dn: $mailalternateaddress");
650                                        }
651                                }
652                                foreach ($remove_mailalternateaddress as $index=>$mailalternateaddress)
653                                {
654                                        if ($mailalternateaddress != '')
655                                        {
656                                                if ($index !== 'count')
657                                                {
658                                                        $ldap_remove['mailalternateaddress'][] = $mailalternateaddress;
659                                                        $this->db_functions->write_log("removed mailalternateaddress","$dn: $mailalternateaddress");
660                                                }
661                                        }
662                                }
663                               
664                                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
665                                // MAILFORWARDINGADDRESS
666                                if (!$new_values['mailforwardingaddress'])
667                                        $new_values['mailforwardingaddress'] = array();
668                                if (!$old_values['mailforwardingaddress'])
669                                        $old_values['mailforwardingaddress'] = array();
670                                $add_mailforwardingaddress = array_diff($new_values['mailforwardingaddress'], $old_values['mailforwardingaddress']);
671                                $remove_mailforwardingaddress = array_diff($old_values['mailforwardingaddress'], $new_values['mailforwardingaddress']);
672                                foreach ($add_mailforwardingaddress as $index=>$mailforwardingaddress)
673                                {
674                                        if ($mailforwardingaddress != '')
675                                        {
676                                                $ldap_add['mailforwardingaddress'][] = $mailforwardingaddress;
677                                                $this->db_functions->write_log("added mailforwardingaddress","$dn: $mailforwardingaddress");
678                                        }
679                                }
680                                foreach ($remove_mailforwardingaddress as $index=>$mailforwardingaddress)
681                                {
682                                        if ($mailforwardingaddress != '')
683                                        {
684                                                if ($index !== 'count')
685                                                {
686                                                        $ldap_remove['mailforwardingaddress'][] = $mailforwardingaddress;
687                                                        $this->db_functions->write_log("removed mailforwardingaddress","$dn: $mailforwardingaddress");
688                                                }
689                                        }
690                                }
691                               
692                                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
693                                // Delivery Mode
694                                if (($old_values['deliverymode'] == '') && ($new_values['deliverymode'] != ''))
695                                {
696                                        $ldap_add['deliverymode'] = 'forwardOnly';
697                                        $this->db_functions->write_log("added forwardOnly", $dn);
698                                }
699                                if (($old_values['deliverymode'] != '') && ($new_values['deliverymode'] == ''))
700                                {
701                                        $ldap_remove['deliverymode'] = array();
702                                        $this->db_functions->write_log("removed forwardOnly", $dn);
703                                }
704                        }
705                       
706                        if ( ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_users')) ||
707                             ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'change_users_quote')) )
708                        {
709                                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
710                                // MAILQUOTA
711                                if ( ($new_values['mailquota'] != $old_values['mailquota']) && (is_numeric($new_values['mailquota'])) )
712                                {
713                                        $result_change_user_quota = $this->imap_functions->change_user_quota($new_values['uid'], $new_values['mailquota']);
714                                       
715                                        if ($result_change_user_quota['status'])
716                                        {
717                                                $this->db_functions->write_log("modified user email quota" , $dn . ':' . $old_values['mailquota'] . '->' . $new_values['mailquota']);
718                                        }
719                                        else
720                                        {
721                                                $return['status'] = false;
722                                                $return['msg'] .= $result_change_user_quota['msg'];
723                                        }
724                                }
725                        }
726
727                        if ( ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_users')) ||
728                             ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_sambausers_attributes')) )
729                        {
730                                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
731                                // REMOVE ATTRS OF SAMBA
732                                if (($this->current_config['expressoAdmin_samba_support'] == 'true') && ($new_values['userSamba']) && ($new_values['use_attrs_samba'] != 'on'))
733                                {
734                                        $ldap_remove['objectclass']                     = 'sambaSamAccount';   
735                                        $ldap_remove['loginShell']                              = array();
736                                        $ldap_remove['sambaSID']                                = array();
737                                        $ldap_remove['sambaPrimaryGroupSID']    = array();
738                                        $ldap_remove['sambaAcctFlags']                  = array();
739                                        $ldap_remove['sambaLogonScript']                = array();
740                                        $ldap_remove['sambaLMPassword']                 = array();
741                                        $ldap_remove['sambaNTPassword']                 = array();
742                                        $ldap_remove['sambaPasswordHistory']    = array();
743                                        $ldap_remove['sambaPwdCanChange']               = array();
744                                        $ldap_remove['sambaPwdLastSet']                 = array();
745                                        $ldap_remove['sambaPwdMustChange']              = array();
746                                        $this->db_functions->write_log("removed user samba attributes", $dn);
747                                }
748                                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
749                                // ADD ATTRS OF SAMBA
750                                if (($this->current_config['expressoAdmin_samba_support'] == 'true') && (!$new_values['userSamba']) && ($new_values['use_attrs_samba'] == 'on'))
751                                {
752                                        if (!is_file('/home/expressolivre/mkntpwd'))
753                                        {
754                                                $return['status'] = false;
755                                                $return['msg'] .= $this->functions->lang("The file /home/expressolivre/mkntpwd does not exist") . ".\n";
756                                                $return['msg'] .= $this->functions->lang("It is necessery to create samba passwords") . ".\n";
757                                                $return['msg'] .= $this->functions->lang("Inform your system administrator about this") . ".\n";
758                                        }
759                                        else
760                                        {
761                                                $ldap_add['objectClass'][]                      = 'sambaSamAccount';
762                                                $ldap_mod_replace['loginShell']         = '/bin/bash';
763                                                $ldap_add['sambaSID']                           = $new_values['sambadomain'] . '-' . ((2 * $new_values['uidnumber'])+1000);
764                                                $ldap_add['sambaPrimaryGroupSID']       = $new_values['sambadomain'] . '-' . ((2 * $new_values['gidnumber'])+1001);
765                                                $ldap_add['sambaAcctFlags']                     = $new_values['sambaacctflags'];
766                                                $ldap_add['sambaLogonScript']           = $new_values['sambalogonscript'];
767                                                $ldap_mod_replace['homeDirectory']      = $new_values['sambahomedirectory'];
768                                                $ldap_add['sambaLMPassword']            = exec('/home/expressolivre/mkntpwd -L '.'senha');
769                                                $ldap_add['sambaNTPassword']            = exec('/home/expressolivre/mkntpwd -N '.'senha');
770                                                $ldap_add['sambaPasswordHistory']       = '0000000000000000000000000000000000000000000000000000000000000000';
771                                                $ldap_add['sambaPwdCanChange']          = strtotime("now");
772                                                $ldap_add['sambaPwdLastSet']            = strtotime("now");
773                                                $ldap_add['sambaPwdMustChange'] = '2147483647';
774                                                $this->db_functions->write_log("added user samba attribute", $dn);
775                                        }
776                                }
777                        }
778                       
779                        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
780                        // GROUPS
781                        if ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_groups'))
782                        {
783                                // If the manager does not have the suficient access, the new_values.uid is empty.
784                                if (empty($new_values['uid']))
785                                        $user_uid = $old_values['uid'];
786                                else
787                                        $user_uid = $new_values['uid'];
788                               
789                                if (!$new_values['groups'])
790                                        $new_values['groups'] = array();
791                                if (!$old_values['groups'])
792                                        $old_values['groups'] = array();
793                       
794                                $add_groups = array_diff($new_values['groups'], $old_values['groups']);
795                                $remove_groups = array_diff($old_values['groups'], $new_values['groups']);
796                       
797                                if (count($add_groups)>0)
798                                {
799                                        foreach($add_groups as $gidnumber)
800                                        {
801                                                $this->db_functions->add_user2group($gidnumber, $new_values['uidnumber']);
802                                                $this->ldap_functions->add_user2group($gidnumber, $user_uid);
803                                                $this->db_functions->write_log("included user to group", "uid:$user_uid -> gid:$gidnumber");
804                                        }
805                                }
806                               
807                                if (count($remove_groups)>0)
808                                {
809                                        foreach($remove_groups as $gidnumber)
810                                        {
811                                                foreach($old_values['groups_info'] as $group)
812                                                {
813                                                        if (($group['gidnumber'] == $gidnumber) && ($group['group_disabled'] == 'false'))
814                                                        {
815                                                                $this->db_functions->remove_user2group($gidnumber, $new_values['uidnumber']);
816                                                                $this->ldap_functions->remove_user2group($gidnumber, $user_uid);
817                                                                $this->db_functions->write_log("removed user from group", "$dn: $gidnumber");
818                                                        }
819                                                }
820                                        }
821                                }
822                        }
823                        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
824                        // LDAP_MOD_REPLACE
825                        if (count($ldap_mod_replace))
826                        {
827                                $result = $this->ldap_functions->replace_user_attributes($dn, $ldap_mod_replace);
828                                if (!$result['status'])
829                                {
830                                        $return['status'] = false;
831                                        $return['msg'] .= $result['msg'];
832                                }
833                        }
834                       
835                        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
836                        // LDAP_MOD_ADD
837                        if (count($ldap_add))
838                        {
839                                $result = $this->ldap_functions->add_user_attributes($dn, $ldap_add);
840                                if (!$result['status'])
841                                {
842                                        $return['status'] = false;
843                                        $return['msg'] .= $result['msg'];
844                                }
845                        }
846                       
847                        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
848                        // LDAP_MOD_REMOVE                     
849                        if (count($ldap_remove))
850                        {
851                                $result = $this->ldap_functions->remove_user_attributes($dn, $ldap_remove);
852                                if (!$result['status'])
853                                {
854                                        $return['status'] = false;
855                                        $return['msg'] .= $result['msg'];
856                                }
857                        }
858                        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
859
860
861                        if ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'edit_users'))
862                        {
863                                ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
864                                // MAILLISTS
865                                if (!$new_values['maillists'])
866                                        $new_values['maillists'] = array();
867                                if (!$old_values['maillists'])
868                                        $old_values['maillists'] = array();
869
870                                $add_maillists = array_diff($new_values['maillists'], $old_values['maillists']);
871                                $remove_maillists = array_diff($old_values['maillists'], $new_values['maillists']);
872                               
873                                if (count($add_maillists)>0)
874                                {
875                                        foreach($add_maillists as $uid)
876                                        {
877                                                $this->ldap_functions->add_user2maillist($uid, $new_values['mail']);
878                                                $this->db_functions->write_log("included user to maillist","$uid: $dn");
879                                        }
880                                }
881
882                                if (count($remove_maillists)>0)
883                                {
884                                        foreach($remove_maillists as $uid)
885                                        {
886                                                $this->ldap_functions->remove_user2maillist($uid, $new_values['mail']);
887                                                $this->db_functions->write_log("removed user from maillist","$dn: $uid");
888                                        }
889                                }
890                       
891                                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
892                                // APPS
893                                $new_values2 = array();
894                                $old_values2 = array();
895                                if (count($new_values['apps'])>0)
896                                {
897                                        foreach ($new_values['apps'] as $app=>$tmp)
898                                        {
899                                                $new_values2[] = $app;
900                                        }
901                                }
902                                if (count($old_values['apps'])>0)
903                                {
904                                        foreach ($old_values['apps'] as $app=>$tmp)
905                                        {
906                                                $old_values2[] = $app;
907                                        }
908                                }
909                                $add_apps    = array_flip(array_diff($new_values2, $old_values2));
910                                $remove_apps = array_flip(array_diff($old_values2, $new_values2));
911
912                                if (count($add_apps)>0)
913                                {
914                                        $this->db_functions->add_id2apps($new_values['uidnumber'], $add_apps);
915
916                                        foreach ($add_apps as $app => $index)
917                                                $this->db_functions->write_log("added application to user","$dn: $app");
918                                }
919                                if (count($remove_apps)>0)
920                                {
921                                        //Verifica se o gerente tem acesso a aplicação antes de remove-la do usuario.
922                                        $manager_apps = $this->db_functions->get_apps($_SESSION['phpgw_session']['session_lid']);
923                                       
924                                        foreach ($remove_apps as $app => $app_index)
925                                        {
926                                                if ($manager_apps[$app] == 'run')
927                                                        $remove_apps2[$app] = $app_index;
928                                        }
929                                        $this->db_functions->remove_id2apps($new_values['uidnumber'], $remove_apps2);
930                                       
931                                        foreach ($remove_apps2 as $app => $access)
932                                                $this->db_functions->write_log("removed application to user","$dn: $app");
933                                }
934                                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
935                        }                       
936                        return $return;
937                }               
938               
939                function get_user_info($uidnumber)
940                {
941                        if (!$user_info_ldap = $this->ldap_functions->get_user_info($uidnumber))
942                                return false;
943                        $user_info_db1 = $this->db_functions->get_user_info($uidnumber);
944                        $user_info_db2 = $this->ldap_functions->gidnumbers2cn($user_info_db1['groups']);
945                        $user_info_imap = $this->imap_functions->get_user_info($user_info_ldap['uid']);
946                        $user_info = array_merge($user_info_ldap, $user_info_db1, $user_info_db2, $user_info_imap);
947                        return $user_info;
948                }
949               
950                function set_user_default_password($params)
951                {
952                        $return['status'] = 1;
953                        $uid = $params['uid'];
954                        $defaultUserPassword = '{md5}'.base64_encode(pack("H*",md5($this->current_config['expressoAdmin_defaultUserPassword'])));
955                       
956                        if (!$this->db_functions->default_user_password_is_set($uid))
957                        {
958                                $userPassword = $this->ldap_functions->set_user_password($uid, $defaultUserPassword);
959                                $this->db_functions->set_user_password($uid, $userPassword);
960                                $this->db_functions->write_log("inserted default password",$uid);
961                        }
962                        else
963                        {
964                                $return['status'] = 0;
965                                $return['msg'] = $this->functions->lang('default password already registered') . '!';
966                        }
967                       
968                        return $return;
969                }
970
971                function return_user_password($params)
972                {
973                        $return['status'] = 1;
974                        $uid = $params['uid'];
975                       
976                        if ($this->db_functions->default_user_password_is_set($uid))
977                        {
978                                $userPassword = $this->db_functions->get_user_password($uid);
979                                $this->ldap_functions->set_user_password($uid, $userPassword);
980                        }
981                        else
982                        {
983                                $return['status'] = 0;
984                                $return['msg'] = $this->functions->lang('default password not registered') . '!';
985                        }
986                       
987                        $this->db_functions->write_log("returned user password",$uid);
988                       
989                        return $return;
990                }
991               
992                function delete($params)
993                {
994                        $return['status'] = true;
995                        $this->db_functions->write_log('delete user: start', $params['uid']);
996                       
997                        // Verifica o acesso do gerente
998                        if ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'delete_users'))
999                        {
1000                                $uidnumber = $params['uidnumber'];
1001                                if (!$user_info = $this->get_user_info($uidnumber))
1002                                {
1003                                        $this->db_functions->write_log('delete user: error getting users info', $user_info['uid']);
1004                                        $return['status'] = false;
1005                                        $return['msg'] = $this->functions->lang('error getting users info');
1006                                        return $return;
1007                                }
1008
1009                                //LDAP
1010                                $result_ldap = $this->ldap_functions->delete_user($user_info);
1011                                if (!$result_ldap['status'])
1012                                {
1013                                        $return['status'] = false;
1014                                        $return['msg'] = 'user.delete(ldap): ' . $result_ldap['msg'];
1015                                        return $return;
1016                                }
1017                                else
1018                                {
1019                                        $this->db_functions->write_log("deleted users data from ldap", $user_info['uid']);
1020                                       
1021                                        //DB
1022                                        $result_db = $this->db_functions->delete_user($user_info);
1023                                        if (!$result_db['status'])
1024                                        {
1025                                                $return['status'] = false;
1026                                                $return['msg'] .= 'user.delete(db): ' . $result_db['msg'];
1027                                        }
1028                                        else
1029                                        {
1030                                                $this->db_functions->write_log("deleted users data from DB", $user_info['uid']);
1031                                        }
1032                                       
1033                                        //IMAP
1034                                        $result_imap = $this->imap_functions->delete_mailbox($user_info['uid']);
1035                                        if (!$result_imap['status'])
1036                                        {
1037                                                $return['status'] = false;
1038                                                $return['msg'] .= $result_imap['msg'];
1039                                        }
1040                                        else
1041                                        {
1042                                                $this->db_functions->write_log("deleted users data from IMAP", $user_info['uid']);
1043                                        }
1044                                       
1045                                }
1046                        }
1047                        else
1048                        {
1049                                $this->db_functions->write_log('delete user: manager does not have access', $params['uidnumber']);
1050                        }
1051                       
1052                        $this->db_functions->write_log('delete user: end', $user_info['uid']);
1053                        return $return;
1054                }
1055
1056
1057                function rename($params)
1058                {
1059                        $return['status'] = true;
1060                       
1061                        // Verifica acesso do gerente (OU) ao tentar renomear um usuário.                       
1062                        if ( ! $this->ldap_functions->check_access_to_renamed($params['uid']) )
1063                        {
1064                                $return['status'] = false;
1065                                $return['msg'] .= $this->functions->lang('You do not have access to delete user') . '.';
1066                                return $return;
1067                        }
1068
1069                        // Check if the new_uid is in use.                     
1070                        if ( ! $this->ldap_functions->check_rename_new_uid($params['new_uid']) )
1071                        {
1072                                $return['status'] = false;
1073                                $return['msg'] = $this->functions->lang('New login already in use') . '.';
1074                                return $return;
1075                        }
1076
1077                        // Verifica o acesso do gerente
1078                        if ($this->functions->check_acl($_SESSION['phpgw_session']['session_lid'], 'rename_users'))
1079                        {
1080                                $uid            = $params['uid'];
1081                                $new_uid        = $params['new_uid'];
1082                                $defaultUserPassword = '{md5}'.base64_encode(pack("H*",md5($this->current_config['expressoAdmin_defaultUserPassword'])));
1083                                $defaultUserPassword_plain = $this->current_config['expressoAdmin_defaultUserPassword'];
1084
1085                                $emailadmin_profiles = $this->db_functions->get_sieve_info();
1086                                $sieve_enable = $emailadmin_profiles[0]['imapenablesieve'];
1087                                $sieve_server = $emailadmin_profiles[0]['imapsieveserver'];
1088                                $sieve_port   = $emailadmin_profiles[0]['imapsieveport'];
1089
1090                                $imap_admin             = $_SESSION['phpgw_info']['expresso']['email_server']['imapAdminUsername'];
1091                                $imap_passwd    = $_SESSION['phpgw_info']['expresso']['email_server']['imapAdminPW'];
1092                                $imap_server    = $_SESSION['phpgw_info']['expresso']['email_server']['imapServer'];
1093                                $imap_port              = $_SESSION['phpgw_info']['expresso']['email_server']['imapPort'];
1094                                $imapDelimiter  = $_SESSION['phpgw_info']['expresso']['email_server']['imapDelimiter'];
1095                       
1096                                //Verifica se está sendo usuado cyrus 2.2 ou superior
1097                                $sk = fsockopen ($imap_server,$imap_port);
1098                                $server_resp = fread($sk, 100);
1099                        $tmp = split('v2.', $server_resp);
1100                        $cyrus_version = '2' . $tmp[1][0];
1101                       
1102                            if ( $cyrus_version < intVal('22') )
1103                {
1104                                        $return['status'] = false;
1105                                        $return['msg'] = "The rename user is only permitted with cyrus 2.2 or higher,";
1106                                        $return['msg'] .= "\nand with the option 'allowusermoves: yes' set in imapd.conf.";
1107
1108
1109                                        return $return;
1110                        }
1111
1112                                // Renomeia UID no openldap
1113                                $result = $this->ldap_functions->rename_uid($uid, $new_uid);
1114                                if (!$result['status'])
1115                                {
1116                                        $return['status'] = false;
1117                                        $return['msg'] = $this->functions->lang("Error rename user in LDAP") . '.';
1118                                        return $return;
1119                                }
1120                               
1121                        //Renomeia mailbox
1122                        $imap_rename_result = $this->imap_functions->rename_mailbox($uid, $new_uid);
1123                                if (!$imap_rename_result['status'])
1124                                {
1125                                        // Back user uid.
1126                                        $result = $this->ldap_functions->rename_uid($new_uid, $uid);
1127                                       
1128                                        $return['status'] = false;
1129                                        $return['msg']  = $this->functions->lang("Error renaming user mailboxes") . ".\n";
1130                                        $return['msg'] .= $imap_rename_result['msg'];
1131                                        return $return;
1132                                }
1133                       
1134
1135                        // Renomeia sieve script
1136                        include_once('sieve-php.lib.php');
1137                        //function sieve($host,         $port,       $user,    $pass,        $auth="",     $auth_types)
1138                        $sieve=new sieve($sieve_server, $sieve_port, $new_uid, $imap_passwd, $imap_admin, 'PLAIN');
1139                               
1140                                if ($sieve->sieve_login())
1141                                {
1142                                        $sieve->sieve_listscripts();
1143                                        $myactivescript=$sieve->response["ACTIVE"];
1144                                        $sieve->sieve_getscript($myactivescript);
1145
1146                                        $script = '';
1147                                        if (!empty($sieve->response))
1148                                        {
1149                                                foreach($sieve->response as $result)
1150                                                {
1151                                                        $script .= $result;
1152                                                }
1153                                        }
1154                                       
1155                                        if (!empty($script))
1156                                        {
1157                                        $scriptname = $new_uid;
1158                                                if($sieve->sieve_sendscript($new_uid,$script))
1159                                                {
1160                                                        if ($sieve->sieve_setactivescript($new_uid))
1161                                                        {
1162                                                                if (!$sieve->sieve_deletescript($myactivescript))
1163                                                                {
1164                                                                        $return['msg'] .= $result['msg'] . $this->functions->lang("Error renaming sieve script") . ".\\n";
1165                                                                        $return['msg'] .= $result['msg'] . $this->functions->lang("Problem deleting old script") . '.';
1166                                                                }
1167                                                        }
1168                                                        else
1169                                                        {
1170                                                                $return['msg'] .= $result['msg'] . $this->functions->lang("Error renaming sieve script") . ".\\n";
1171                                                                $return['msg'] .= $result['msg'] . $this->functions->lang("Problem activating sieve script") . '.';
1172                                                        }
1173                                                }
1174                                                else
1175                                                {
1176                                                        $return['msg'] .= $result['msg'] . $this->functions->lang("Error renaming sieve script") . ".\\n";
1177                                                        $return['msg'] .= $result['msg'] . $this->functions->lang("Problem saving sieve script") . '.';
1178                                                }
1179                                        }
1180                                        $sieve->sieve_logout();
1181                                }
1182                                else
1183                                {
1184                                                $return['status'] = false;
1185                                                $return['msg'] .= $result['msg'] . $this->functions->lang("Error renaming sieve script") . ".\\n";
1186                                                $return['msg'] .= $result['msg'] . $this->functions->lang("Can not login sieve") . '.';
1187                                }
1188
1189                                $this->db_functions->write_log("renamed user", "$uid -> $new_uid");
1190
1191                                $return['exec_return'] = "";
1192
1193                        return $return;
1194                        }
1195                }
1196               
1197                function write_log_from_ajax($params)
1198                {
1199                        $this->db_functions->write_log($params['_action'],'',$params['userinfo'],'','');
1200                        return true;
1201                }
1202        }
1203?>
Note: See TracBrowser for help on using the repository browser.