source: trunk/setup/ldapimport.php @ 7673

Revision 7673, 16.2 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Correcoes para Performance: Function Within Loop Declaration.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2  /**************************************************************************\
3  * eGroupWare - Setup                                                       *
4  * http://www.egroupware.org                                                *
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
13        $phpgw_info = array();
14        $phpgw_info["flags"] = array(
15                'noheader'   => True,
16                'nonavbar'   => True,
17                'currentapp' => 'home',
18                'noapi'      => True
19        );
20        include('./inc/functions.inc.php');
21
22        // Authorize the user to use setup app and load the database
23        if(!$GLOBALS['phpgw_setup']->auth('Config'))
24        {
25                Header('Location: index.php');
26                exit;
27        }
28        // Does not return unless user is authorized
29
30        class phpgw
31        {
32                var $common;
33                var $accounts;
34                var $applications;
35                var $db;
36        }
37        $phpgw = new phpgw;
38        $phpgw->common = CreateObject('phpgwapi.common');
39
40        $common = $phpgw->common;
41        $GLOBALS['phpgw_setup']->loaddb();
42        $phpgw->db = $GLOBALS['phpgw_setup']->db;
43
44        $tpl_root = $GLOBALS['phpgw_setup']->html->setup_tpl_dir('setup');
45        $setup_tpl = CreateObject('setup.Template',$tpl_root);
46        $setup_tpl->set_file(array(
47                'ldap'   => 'ldap.tpl',
48                'T_head' => 'head.tpl',
49                'T_footer' => 'footer.tpl',
50                'T_alert_msg' => 'msg_alert_msg.tpl'
51        ));
52
53        $phpgw_info['server']['auth_type'] = 'ldap';
54
55        $phpgw->applications = CreateObject('phpgwapi.applications');
56        $applications        = $phpgw->applications;
57
58        $GLOBALS['phpgw_setup']->db->query("SELECT config_name,config_value FROM phpgw_config WHERE config_app='phpgwapi' and config_name LIKE 'ldap%' OR config_name='account_repository'",__LINE__,__FILE__);
59        while($GLOBALS['phpgw_setup']->db->next_record())
60        {
61                $config[$GLOBALS['phpgw_setup']->db->f('config_name')] = $GLOBALS['phpgw_setup']->db->f('config_value');
62        }
63        $phpgw_info['server']['ldap_host']          = $config['ldap_host'];
64        $phpgw_info['server']['ldap_context']       = $config['ldap_context'];
65        $phpgw_info['server']['ldap_group_context'] = $config['ldap_group_context'];
66        $phpgw_info['server']['ldap_root_dn']       = $config['ldap_root_dn'];
67        $phpgw_info['server']['ldap_root_pw']       = $config['ldap_root_pw'];
68        $phpgw_info['server']['account_repository'] = $config['account_repository'];
69
70        $phpgw->accounts     = CreateObject('phpgwapi.accounts');
71        $acct                = $phpgw->accounts;
72
73        // First, see if we can connect to the LDAP server, if not send `em back to config.php with an
74        // error message.
75
76        // connect to ldap server
77        if(!$ldap = $common->ldapConnect())
78        {
79                $noldapconnection = True;
80        }
81
82        if($noldapconnection)
83        {
84                Header('Location: config.php?error=badldapconnection');
85                exit;
86        }
87
88        $sr = ldap_search($ldap,$config['ldap_context'],'(|(uid=*))',array('sn','givenname','uid','uidnumber'));
89        $info = ldap_get_entries($ldap, $sr);
90        $tmp = '';
91
92        for($i=0; $i<$info['count']; ++$i)
93        {
94                if(!$phpgw_info['server']['global_denied_users'][$info[$i]['uid'][0]])
95                {
96                        $tmp = $info[$i]['uidnumber'][0];
97                        $account_info[$tmp]['account_id']        = $info[$i]['uidnumber'][0];
98                        $account_info[$tmp]['account_lid']       = $info[$i]['uid'][0];
99                        $account_info[$tmp]['account_firstname'] = $info[$i]['givenname'][0];
100                        $account_info[$tmp]['account_lastname']  = $info[$i]['sn'][0];
101                        $account_info[$tmp]['account_passwd']    = $info[$i]['userpassword'][0];
102                }
103        }
104
105        if($phpgw_info['server']['ldap_group_context'])
106        {
107                $srg = ldap_search($ldap,$config['ldap_group_context'],'(|(cn=*))',array('gidnumber','cn','memberuid'));
108                $info = ldap_get_entries($ldap, $srg);
109                $tmp = '';
110
111                for($i=0; $i<$info['count']; ++$i)
112                {
113                        if(!$phpgw_info['server']['global_denied_groups'][$info[$i]['cn'][0]] &&
114                                !$account_info[$i][$info[$i]['cn'][0]])
115                        {
116                                $tmp = $info[$i]['gidnumber'][0];
117                                $group_info[$tmp]['account_id']        = $info[$i]['gidnumber'][0];
118                                $group_info[$tmp]['account_lid']       = $info[$i]['cn'][0];
119                                $group_info[$tmp]['members']           = $info[$i]['memberuid'];
120                                $group_info[$tmp]['account_firstname'] = $info[$i]['cn'][0];
121                                $group_info[$tmp]['account_lastname']  = 'Group';
122                        }
123                }
124        }
125        else
126        {
127                $group_info = array();
128        }
129
130        $GLOBALS['phpgw_setup']->db->query("SELECT app_name FROM phpgw_applications WHERE app_enabled!='0' AND app_enabled!='3' ORDER BY app_name",__LINE__,__FILE__);
131        while($GLOBALS['phpgw_setup']->db->next_record())
132        {
133                $apps[$GLOBALS['phpgw_setup']->db->f('app_name')] = lang($GLOBALS['phpgw_setup']->db->f('app_name'));
134        }
135
136        $cancel = get_var('cancel','POST');
137        $submit = get_var('submit','POST');
138        $users  = get_var('users','POST');
139        $admins = get_var('admins','POST');
140        $s_apps = get_var('s_apps','POST');
141        $ldapgroups = get_var('ldapgroups','POST');
142
143        if($cancel)
144        {
145                Header('Location: ldap.php');
146                exit;
147        }
148
149        if($submit)
150        {
151                if(!count($admins))
152                {
153                        $error = '<br>You must select at least 1 admin';
154                }
155
156                if(!count($s_apps))
157                {
158                        $error .= '<br>You must select at least 1 application';
159                }
160
161                if(!$error)
162                {
163                        if($users)
164                        {
165                                while(list($key,$id) = each($users))
166                                {
167                                        $id_exist = 0;
168                                        $thisacctid    = $account_info[$id]['account_id'];
169                                        $thisacctlid   = $account_info[$id]['account_lid'];
170                                        $thisfirstname = $account_info[$id]['account_firstname'];
171                                        $thislastname  = $account_info[$id]['account_lastname'];
172                                        $thispasswd    = $account_info[$id]['account_passwd'];
173
174                                        // Do some checks before we try to import the data.
175                                        if(!empty($thisacctid) && !empty($thisacctlid))
176                                        {
177                                                $accounts = CreateObject('phpgwapi.accounts',(int)$thisacctid);
178                                                copyobj($GLOBALS['phpgw_setup']->db,$accounts->db);
179
180                                                // Check if the account is already there.
181                                                // If so, we won't try to create it again.
182                                                $acct_exist = $acct->name2id($thisacctlid);
183                                                if($acct_exist)
184                                                {
185                                                        $thisacctid = $acct_exist;
186                                                }
187                                                $id_exist = $accounts->exists($thisacctlid);
188                                                // If not, create it now.
189                                                if(!$id_exist)
190                                                {
191                                                        $thisaccount_info = array(
192                                                                'account_type'      => 'u',
193                                                                'account_lid'       => $thisacctlid,
194                                                                'account_passwd'    => 'x',
195                                                        /*      'account_passwd'    => $thispasswd, */
196                                                                'account_firstname' => $thisfirstname,
197                                                                'account_lastname'  => $thislastname,
198                                                                'account_status'    => 'A',
199                                                                'account_expires'   => -1
200                                                        );
201                                                        $thisacctid = $accounts->create($thisaccount_info);
202                                                }
203                                                if (!$thisacctid)       // if we have no account_id, we cant continue
204                                                {
205                                                        continue;
206                                                }
207                                                // Insert default acls for this user.
208                                                // Since the group has app rights, we don't need to give users
209                                                //  these rights.  Instead, we make the user a member of the Default group
210                                                //  below.
211                                                $acl = CreateObject('phpgwapi.acl',(int)$thisacctid);
212                                                $acl->db = $GLOBALS['phpgw_setup']->db;
213                                                $acl->read_repository();
214
215                                                // Only give them admin if we asked for them to have it.
216                                                // This is typically an exception to apps for run rights
217                                                //  as a group member.
218                        $admins_count = count($admins);
219                                                for($a=0;$a<$admins_count;++$a)
220                                                {
221                                                        if($admins[$a] == $thisacctlid)
222                                                        {
223                                                                $acl->delete('admin','run',1);
224                                                                $acl->add('admin','run',1);
225                                                        }
226                                                }
227
228                                                // Now make them a member of the 'Default' group.
229                                                // But, only if the current user is not the group itself.
230                                                if(!$defaultgroupid)
231                                                {
232                                                        $defaultgroupid = $accounts->name2id('Default');
233                                                }
234                                                if($defaultgroupid)
235                                                {
236                                                        $acl->delete('phpgw_group',$defaultgroupid,1);
237                                                        $acl->add('phpgw_group',$defaultgroupid,1);
238                                                }
239
240                                                // Save these new acls.
241                                                $acl->save_repository();
242                                        }
243                                }
244                        }
245
246                        if($ldapgroups)
247                        {
248                                while(list($key,$groupid) = each($ldapgroups))
249                                {
250                                        $id_exist = 0;
251                                        $thisacctid    = $group_info[$groupid]['account_id'];
252                                        $thisacctlid   = $group_info[$groupid]['account_lid'];
253                                        $thisfirstname = $group_info[$groupid]['account_firstname'];
254                                        $thislastname  = $group_info[$groupid]['account_lastname'];
255                                        $thismembers   = $group_info[$groupid]['members'];
256
257                                        // Do some checks before we try to import the data.
258                                        if(!empty($thisacctid) && !empty($thisacctlid))
259                                        {
260                                                $groups = CreateObject('phpgwapi.accounts',(int)$thisacctid);
261                                                copyobj($GLOBALS['phpgw_setup']->db,$groups->db);
262
263                                                // Check if the account is already there.
264                                                // If so, we won't try to create it again.
265                                                $acct_exist = $groups->name2id($thisacctlid);
266                                                /* echo '<br<group: ' . $acct_exist; */
267                                                if($acct_exist)
268                                                {
269                                                        $thisacctid = $acct_exist;
270                                                }
271                                                $id_exist = $groups->exists((int)$thisacctid);
272                                                // If not, create it now.
273                                                if(!$id_exist)
274                                                {
275                                                        $thisgroup_info = array(
276                                                                'account_type'      => 'g',
277                                                                'account_lid'       => $thisacctlid,
278                                                                'account_passwd'    => $passwd,
279                                                                'account_firstname' => $thisfirstname,
280                                                                'account_lastname'  => $thislastname,
281                                                                'account_status'    => 'A',
282                                                                'account_expires'   => -1
283                                                        );
284                                                        $thisacctid = $groups->create($thisgroup_info);
285                                                }
286                                                if (!$thisacctid)       // if we have no account_id, we cant continue
287                                                {
288                                                        continue;
289                                                }
290                                                // Now make them a member of this group in phpgw.
291                                                while(list($key,$members) = each($thismembers))
292                                                {
293                                                        if($key == 'count')
294                                                        {
295                                                                continue;
296                                                        }
297                                                        /* echo '<br>members: ' . $members; */
298                                                        $tmpid = 0;
299                                                        @reset($account_info);
300                                                        while(list($x,$y) = each($account_info))
301                                                        {
302                                                                /* echo '<br>checking: '.$y['account_lid']; */
303                                                                if($members == $y['account_lid'])
304                                                                {
305                                                                        $tmpid = $acct->name2id($y['account_lid']);
306                                                                }
307                                                        }
308                                                        /*
309                                                        Insert acls for this group based on memberuid field.
310                                                        Since the group has app rights, we don't need to give users
311                                                        these rights.  Instead, we maintain group membership here.
312                                                        */
313                                                        if($tmpid)
314                                                        {
315                                                                $acl = CreateObject('phpgwapi.acl',$tmpid);
316                                                                copyobj($GLOBALS['phpgw_setup']->db,$acl->db);
317                                                                $acl->account_id = (int)$tmpid;
318                                                                $acl->read_repository();
319
320                                                                $acl->delete('phpgw_group',$thisacctid,1);
321                                                                $acl->add('phpgw_group',$thisacctid,1);
322
323                                                                /* Now add the acl to let them change their password */
324                                                                $acl->delete('preferences','changepassword',1);
325                                                                $acl->add('preferences','changepassword',1);
326
327                                                                $acl->save_repository();
328
329                                                                /* Add prefs for selected apps here, since they are per-user.
330                                                                        App access is added below.
331                                                                */
332                                                                $pref = CreateObject('phpgwapi.preferences',$tmpid);
333                                                                $pref->db = $GLOBALS['phpgw_setup']->db;
334                                                                $pref->account_id = (int)$tmpid;
335                                                                $pref->read_repository();
336                                                                @reset($s_apps);
337                                                                while(list($key,$app) = each($s_apps))
338                                                                {
339                                                                        $phpgw->hooks->single('add_def_pref',$app);
340                                                                }
341                                                                $pref->save_repository();
342                                                        }
343                                                }
344                                                /* Now give this group some rights */
345                                                $phpgw_info['user']['account_id'] = $thisacctid;
346                                                $acl = CreateObject('phpgwapi.acl');
347                                                copyobj($GLOBALS['phpgw_setup']->db,$acl->db);
348                                                $acl->account_id = (int)$thisacctid;
349                                                $acl->read_repository();
350                                                @reset($s_apps);
351                                                while(list($key,$app) = each($s_apps))
352                                                {
353                                                        $acl->delete($app,'run',1);
354                                                        $acl->add($app,'run',1);
355                                                }
356                                                $acl->save_repository();
357                                                $defaultgroupid = $thisacctid;
358                                        }
359                                }
360                        }
361                        else
362                        {
363                                /* Create the 'Default' group */
364                                $groups = CreateObject('phpgwapi.accounts',$defaultgroupid);
365                                copyobj($GLOBALS['phpgw_setup']->db,$groups->db);
366
367                                // Check if the group account is already there.
368                                // If so, set our group_id to that account's id for use below.
369                                $acct_exist = $groups->name2id('Default');
370                                if($acct_exist)
371                                {
372                                        $defaultgroupid = $acct_exist;
373                                }
374                                $id_exist   = $groups->exists((int)$defaultgroupid);
375                                // if not, create it, using our original groupid.
376                                if($id_exist)
377                                {
378                                        $groups->delete($defaultgroupid);
379                                }
380                                $thisgroup_info = array(
381                                        'account_type'      => 'g',
382                                        'account_lid'       => 'Default',
383                                        'account_passwd'    => $passwd,
384                                        'account_firstname' => 'Default',
385                                        'account_lastname'  => 'Group',
386                                        'account_status'    => 'A',
387                                        'account_expires'   => -1
388                                );
389                                $acct->create($thisgroup_info);
390
391                                $defaultgroupid = $acct->name2id('Default');
392
393                                $acl = CreateObject('phpgwapi.acl',$defaultgroupid);
394                                copyobj($GLOBALS['phpgw_setup']->db,$acl->db);
395                                $acl->account_id = (int)$defaultgroupid;
396                                $acl->read_repository();
397                                @reset($s_apps);
398                                while(list($key,$app) = each($s_apps))
399                                {
400                                        $acl->delete($app,'run',1);
401                                        $acl->add($app,'run',1);
402                                }
403                                $acl->save_repository();
404                        } //end default group creation
405                }
406                $setup_complete = True;
407        }
408
409        $GLOBALS['phpgw_setup']->html->show_header(lang('LDAP Import'),False,'config',$GLOBALS['phpgw_setup']->ConfigDomain . '(' . $phpgw_domain[$GLOBALS['phpgw_setup']->ConfigDomain]['db_type'] . ')');
410
411        if($error)
412        {
413                //echo '<br><center><b>Error:</b> '.$error.'</center>';
414                $GLOBALS['phpgw_setup']->html->show_alert_msg('Error',$error);
415        }
416
417        if($setup_complete)
418        {
419                echo '<br><center>'.lang('Import has been completed!').' '.lang('Click <a href="index.php">here</a> to return to setup.').'</center>';
420                $GLOBALS['phpgw_setup']->html->show_footer();
421                exit;
422        }
423
424        $setup_tpl->set_block('ldap','header','header');
425        $setup_tpl->set_block('ldap','user_list','user_list');
426        $setup_tpl->set_block('ldap','admin_list','admin_list');
427        $setup_tpl->set_block('ldap','group_list','group_list');
428        $setup_tpl->set_block('ldap','app_list','app_list');
429        $setup_tpl->set_block('ldap','submit','submit');
430        $setup_tpl->set_block('ldap','footer','footer');
431
432        while(list($key,$account) = each($account_info))
433        {
434                $user_list .= '<option value="' . $account['account_id'] . '">'
435                        . $common->display_fullname($account['account_lid'],$account['account_firstname'],$account['account_lastname'])
436                        . '</option>';
437        }
438
439        @reset($account_info);
440        while(list($key,$account) = each($account_info))
441        {
442                $admin_list .= '<option value="' . $account['account_lid'] . '">'
443                        . $common->display_fullname($account['account_lid'],$account['account_firstname'],$account['account_lastname'])
444                        . '</option>';
445        }
446
447        while(list($key,$group) = each($group_info))
448        {
449                $group_list .= '<option value="' . $group['account_id'] . '">'
450                        . $group['account_lid']
451                        . '</option>';
452        }
453
454        while(list($appname,$apptitle) = each($apps))
455        {
456                if($appname == 'admin' ||
457                        $appname == 'skel' ||
458                        $appname == 'backup' ||
459                        $appname == 'netsaint' ||
460                        $appname == 'developer_tools' ||
461                        $appname == 'phpsysinfo' ||
462                        $appname == 'eldaptir' ||
463                        $appname == 'qmailldap')
464                {
465                        $app_list .= '<option value="' . $appname . '">' . $apptitle . '</option>';
466                }
467                else
468                {
469                        $app_list .= '<option value="' . $appname . '" selected>' . $apptitle . '</option>';
470                }
471        }
472
473        $setup_tpl->set_var('action_url','ldapimport.php');
474        $setup_tpl->set_var('users',$user_list);
475        $setup_tpl->set_var('admins',$admin_list);
476        $setup_tpl->set_var('ldapgroups',$group_list);
477        $setup_tpl->set_var('s_apps',$app_list);
478
479        $setup_tpl->set_var('ldap_import',lang('LDAP import users'));
480        $setup_tpl->set_var('description',lang("This section will help you import users and groups from your LDAP tree into eGroupWare's account tables").'.');
481        $setup_tpl->set_var('select_users',lang('Select which user(s) will be imported'));
482        $setup_tpl->set_var('select_admins',lang('Select which user(s) will have admin privileges'));
483        $setup_tpl->set_var('select_groups',lang('Select which group(s) will be imported (group membership will be maintained)'));
484        $setup_tpl->set_var('select_apps',lang('Select the default applications to which your users will have access').'.');
485        $setup_tpl->set_var('note',lang('Note: You will be able to customize this later').'.');
486        $setup_tpl->set_var('form_submit','import');
487        $setup_tpl->set_var('cancel',lang('Cancel'));
488
489        $setup_tpl->pfp('out','header');
490        $setup_tpl->pfp('out','user_list');
491        $setup_tpl->pfp('out','admin_list');
492        $setup_tpl->pfp('out','group_list');
493        $setup_tpl->pfp('out','app_list');
494        $setup_tpl->pfp('out','submit');
495        $setup_tpl->pfp('out','footer');
496
497        $GLOBALS['phpgw_setup']->html->show_footer();
498?>
Note: See TracBrowser for help on using the repository browser.