source: trunk/expressoAdmin1_2/inc/class.user.inc.php @ 507

Revision 507, 44.7 KB checked in by niltonneto, 16 years ago (diff)

Melhorado função rename_user.
Não é mais preciso "alterar" a senha do usuário.

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