Index: /branches/2.2/setup/ldapmodify.php
===================================================================
--- /branches/2.2/setup/ldapmodify.php	(revision 3584)
+++ /branches/2.2/setup/ldapmodify.php	(revision 3607)
@@ -51,9 +51,12 @@
 	));
 
-	$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__);
-	while($GLOBALS['phpgw_setup']->db->next_record())
-	{
-		$config[$GLOBALS['phpgw_setup']->db->f('config_name')] = $GLOBALS['phpgw_setup']->db->f('config_value');
-	}
+	$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__
+	);
+
+	while ( $GLOBALS[ 'phpgw_setup' ] -> db -> next_record( ) )
+		$config[ $GLOBALS[ 'phpgw_setup' ] -> db -> f( 'config_name' ) ] = $GLOBALS[ 'phpgw_setup' ] -> db -> f( 'config_value' );
 
 	$phpgw_info['server']['ldap_host']          = $config['ldap_host'];
@@ -68,220 +71,257 @@
 	$acct            = $phpgw->accounts;
 
-	/* connect to ldap server */
-	if(!$ldap = $common->ldapConnect())
-	{
-		$noldapconnection = True;
-	}
-
-	if($noldapconnection)
-	{
-		Header('Location: config.php?error=badldapconnection');
+	// connect to ldap server
+	if ( ! $ldap = $common -> ldapConnect( ) )
+	{
+		Header( 'Location: config.php?error=badldapconnection' );
 		exit;
 	}
 
 	// Take the users from LDAP.
-	$sr = ldap_search($ldap,$config['ldap_context'],'(cn=*)',array('cn','givenname','uid','uidnumber'));
-	$info = ldap_get_entries($ldap, $sr);
+	$sr = ldap_search( $ldap, $config[ 'ldap_context' ], '(objectClass=posixAccount)', array( 'cn', 'givenname', 'uid', 'uidnumber', 'objectClass' ) );
+	$info = ldap_get_entries( $ldap, $sr );
 	$tmp = '';
 
-	for($i=0; $i<$info['count']; $i++)
-	{
-		if(!$phpgw_info['server']['global_denied_users'][$info[$i]['uid'][0]])
+	$account_info = array( );
+	for ( $i = 0; $i < $info[ 'count' ]; $i++ )
+		if ( ! array_key_exists( $info[ $i ][ 'uid' ][ 0 ], $phpgw_info[ 'server' ][ 'global_denied_users' ] ) )
+			$account_info[ $info[ $i ][ 'dn' ] ] = $info[ $i ];
+
+	$group_info = array( );
+	if ( array_key_exists( 'ldap_group_context', $phpgw_info[ 'server' ] ) && count( $phpgw_info['server']['global_denied_groups'] ) )
+	{
+		$sr = ldap_search( $ldap, $config[ 'ldap_group_context' ], '(objectClass=posixGroup)', array( 'gidnumber', 'cn', 'memberuid' ) );
+		$info = ldap_get_entries( $ldap, $sr );
+		$tmp = '';
+
+		for ( $i = 0; $i < $info[ 'count' ]; $i++ )
+			if ( ! array_key_exists( $info[ $i ][ 'cn' ][ 0 ], $phpgw_info[ 'server' ][ 'global_denied_groups' ] ) )
+				$group_info[ $info[ $i ][ 'dn' ] ] = $info[ $i ];
+	}
+
+	$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__
+	);
+
+	while( $GLOBALS[ 'phpgw_setup' ] -> db -> next_record( ) )
+		$apps[ $GLOBALS[ 'phpgw_setup' ] -> db -> f( 'app_name' ) ] = lang( $GLOBALS[ 'phpgw_setup' ] -> db -> f( 'app_name' ) );
+
+	if ( $cancel )
+	{
+		Header( 'Location: ldap.php' );
+		exit;
+	}
+
+	$GLOBALS[ 'phpgw_setup' ] -> html -> show_header(
+		lang('LDAP Modify'),
+		false,
+		'config',
+		"{$GLOBALS[ 'phpgw_setup' ] -> ConfigDomain} ( {$phpgw_domain[ $GLOBALS[ 'phpgw_setup' ] -> ConfigDomain ][ 'db_type' ]} )"
+	);
+
+	//error_log( print_r( $config, true ), 3, '/tmp/log' );
+	//error_log( print_r( $group_info, true ), 3, '/tmp/log' );
+
+	if ( array_key_exists( 'submit', $_POST ) )
+	{
+		$acl = CreateObject('phpgwapi.acl');
+		copyobj( $GLOBALS[ 'phpgw_setup' ] -> db, $acl -> db );
+
+		if ( array_key_exists( 'ldapgroups', $_POST ) && is_array( $_POST[ 'ldapgroups' ] ) )
 		{
-			$account_info[$info[$i]['uidnumber'][0]] = $info[$i];
-		}
-	}
-
-	if($phpgw_info['server']['ldap_group_context'])
-	{
-		$srg = ldap_search($ldap,$config['ldap_group_context'],'(&(cn=*)(objectClass=posixGroup))',array('gidnumber','cn','memberuid'));
-		$info = ldap_get_entries($ldap, $srg);
-		$tmp = '';
-
-		for($i=0; $i<$info['count']; $i++)
-		{
-			if(!$phpgw_info['server']['global_denied_groups'][$info[$i]['cn'][0]] &&
-				!$account_info[$i][$info[$i]['cn'][0]])
+			$groups = CreateObject( 'phpgwapi.accounts' );
+			copyobj( $GLOBALS[ 'phpgw_setup' ] -> db, $groups -> db );
+
+			//error_log( print_r( $_POST[ 'ldapgroups' ], true ), 3, '/tmp/log' );
+
+			foreach ( $_POST[ 'ldapgroups' ] as $groupid )
 			{
-				$group_info[$info[$i]['gidnumber'][0]] = $info[$i];
-			}
-		}
-	}
-	else
-	{
-		$group_info = array();
-	}
-
-	$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__);
-	while($GLOBALS['phpgw_setup']->db->next_record())
-	{
-		$apps[$GLOBALS['phpgw_setup']->db->f('app_name')] = lang($GLOBALS['phpgw_setup']->db->f('app_name'));
-	}
-
-	if($cancel)
-	{
-		Header("Location: ldap.php");
-		exit;
-	}
-
-	$GLOBALS['phpgw_setup']->html->show_header(lang('LDAP Modify'),False,'config',$GLOBALS['phpgw_setup']->ConfigDomain . '(' . $phpgw_domain[$GLOBALS['phpgw_setup']->ConfigDomain]['db_type'] . ')');
-	$setup_complete = False;
-	if(isset($_POST['submit']))
-	{
-		$acl = CreateObject('phpgwapi.acl');
-		copyobj($GLOBALS['phpgw_setup']->db,$acl->db);
-		if(isset($_POST['ldapgroups']))
-		{
-			$groups = CreateObject('phpgwapi.accounts');
-			copyobj($GLOBALS['phpgw_setup']->db,$groups->db);
-			while(list($key,$groupid) = each($_POST['ldapgroups']))
-			{
-				$id_exist = 0;
-				$entry = array();
-				$thisacctid    = $group_info[$groupid]['gidnumber'][0];
-				$thisacctlid   = $group_info[$groupid]['cn'][0];
-				/* echo "Updating GROUPID : ".$thisacctlid."<br>\n"; */
-				$thisfirstname = $group_info[$groupid]['cn'][0];
-				$thismembers   = $group_info[$groupid]['memberuid'];
-				$thisdn        = $group_info[$groupid]['dn'];
-
-				/* Do some checks before we try to import the data. */
-				if(!empty($thisacctid) && !empty($thisacctlid))
+				if ( ! array_key_exists( $groupid, $group_info ) )
 				{
-					$groups->account_id = (int)$thisacctid;
-
-					$sr = ldap_search($ldap,$config['ldap_group_context'],'cn='.$thisacctlid);
-					$entry = ldap_get_entries($ldap, $sr);
-
-					reset($entry[0]['objectclass']);
-					$addclass = True;
-					while(list($key,$value) = each($entry[0]['objectclass']))
+					echo "Has occurred some problem in the group : {$groupid}<br>\n";
+					continue;
+				}
+
+				$entry = array( );
+
+				$thisacctid    = $group_info[ $groupid ][ 'gidnumber' ][ 0 ];
+				$thisacctlid   = $group_info[ $groupid ][ 'cn' ][ 0 ];
+				$thisfirstname = $group_info[ $groupid ][ 'cn' ][ 0 ];
+				$thismembers   = $group_info[ $groupid ][ 'memberuid' ];
+				$thisdn        = $group_info[ $groupid ][ 'dn' ];
+
+				echo "Updating GROUPID : {$thisacctlid} ({$groupid})<br>\n";
+
+				// Do some checks before we try to import the data.
+				if ( ! empty( $thisacctid ) && ! empty( $thisacctlid ) )
+				{
+					$groups->account_id = ( int ) $thisacctid;
+
+					$sr = ldap_search( $ldap, $config[ 'ldap_group_context' ], 'cn=' . $thisacctlid );
+					$entry = ldap_get_entries( $ldap, $sr );
+
+			//		error_log( print_r( $entry, true ), 3, '/tmp/log' );
+
+					reset( $entry[ 0 ][ 'objectclass' ] );
+
+					$add = array( );
+
+					if ( ! in_array( 'phpgwAccount', $entry[ 0 ][ 'objectclass' ] ) )
+						$add[ 'objectclass'] = array( 'phpgwAccount' );
+
+					if ( ! array_key_exists( 'phpgwaccountstatus', $entry[ 0 ] ) )
+						$add[ 'phpgwaccountstatus'] = array( 'A' );
+
+					if ( ! array_key_exists( 'phpgwaccounttype', $entry[ 0 ] ) )
+						$add[ 'phpgwaccounttype' ] = array( 'g' );
+
+					if ( ! array_key_exists( 'phpgwaccountexpires', $entry[ 0 ] ) )
+						$add[ 'phpgwaccountexpires' ] = array( -1 );
+
+			//		error_log( print_r( $add, true ), 3, '/tmp/log' );
+
+					//if ( count( $add ) )
+					//	ldap_mod_add( $ldap, $thisdn, $add );
+
+					// Now make the members a member of this group in phpgw.
+					if ( is_array( $thismembers ) )
 					{
-						if(strtolower($value) == 'phpgwaccount')
+						if ( array_key_exists( 'count', $thismembers ) )
+							unset( $thismembers[ 'count' ] );
+
+			//			error_log( print_r( $thismembers, true ), 3, '/tmp/log' );
+			//			error_log( print_r( $account_info, true ), 3, '/tmp/log' );
+
+						foreach ( $thismembers as $key => $members )
 						{
-							$addclass = False;
-						}
-					}
-					if($addclass)
-					{
-						reset($entry[0]['objectclass']);
-						$replace['objectclass'] = $entry[0]['objectclass'];
-						unset($replace['objectclass']['count']);
-						$replace['objectclass'][]       = 'phpgwAccount';
-						sort($replace['objectclass']);
-						ldap_mod_replace($ldap,$thisdn,$replace);
-						unset($replace);
-						unset($addclass);
-					}
-					unset($add);
-					if(!@isset($entry[0]['phpgwaccountstatus']))
-					{
-						$add['phpgwaccountstatus'][] = 'A';
-					}
-					if(!@isset($entry[0]['phpgwaccounttype']))
-					{
-						$add['phpgwaccounttype'][] = 'g';
-					}
-					if(!@isset($entry[0]['phpgwaccountexpires']))
-					{
-						$add['phpgwaccountexpires'][] = -1;
-					}
-					if(@isset($add))
-					{
-						ldap_mod_add($ldap,$thisdn,$add);
-					}
-
-					/* Now make the members a member of this group in phpgw. */
-					if(is_array($thismembers))
-					{
-						foreach($thismembers as $key => $members)
-						{
-							if($key == 'count')
-							{
-								continue;
-							}
-							echo '<br>members: ' . $members;
-							$tmpid = 0;
-							@reset($account_info);
-							while(list($x,$y) = each($account_info))
-							{
-								//echo '<br>checking: '.$y['uid'][0];
-								if($members == $y['uid'][0])
+							echo "members: {$members}<br>\n";
+
+							$tmpid = NULL;
+							foreach ( $account_info as $info )
+								if ( $members == $info[ 'uid' ][ 0 ] )
 								{
-									$tmpid = $y['uidnumber'][0];
+									$tmpid = $info[ 'uidnumber' ][ 0 ];
+									break;
 								}
-							}
+
 							// Insert acls for this group based on memberuid field.
 							// Since the group has app rights, we don't need to give users
-							//  these rights.  Instead, we maintain group membership here.
-							if($tmpid)
+							// these rights. Instead, we maintain group membership here.
+							if ( $tmpid )
 							{
-								echo '<br>inserindo user_id: ' . $tmpid . ' em ' . $thisacctid;
-								$acl->account_id = (int)$tmpid;
-								$acl->read_repository();
+								echo "inserindo user_id: {$tmpid} em {$thisacctid}<br>\n";
+
+								$acl -> account_id = ( int ) $tmpid;
+								$acl -> read_repository( );
 	
-								$acl->delete('phpgw_group',$thisacctid,1);
-								$acl->add('phpgw_group',$thisacctid,1);
+								$acl -> delete( 'phpgw_group', $thisacctid, 1 );
+								$acl -> add( 'phpgw_group', $thisacctid, 1 );
 	
 								// Now add the acl to let them change their password
-								$acl->delete('preferences','changepassword',1);
-								$acl->add('preferences','changepassword',1);
+								$acl -> delete( 'preferences', 'changepassword', 1 );
+								$acl -> add( 'preferences', 'changepassword', 1 );
 	
-								$acl->save_repository();
+								$acl -> save_repository( );
 							}
 						}
 					}
-					/* Now give this group some rights */
-					$phpgw_info['user']['account_id'] = $thisacctid;
-					$acl->account_id = (int)$thisacctid;
-					$acl->read_repository();
-					@reset($_POST['s_apps']);
-					while(list($key,$app) = @each($_POST['s_apps']))
+
+					// Now give this group some rights
+					$phpgw_info[ 'user' ][ 'account_id' ] = $thisacctid;
+
+					$acl -> account_id = ( int ) $thisacctid;
+					$acl -> read_repository( );
+
+					foreach ( $_POST[ 's_apps' ] as $app )
 					{
-						$acl->delete($app,'run',1);
-						$acl->add($app,'run',1);
+						$acl -> delete( $app, 'run', 1 );
+						$acl -> add( $app, 'run', 1 );
 					}
-					$acl->save_repository();
+
+					$acl -> save_repository();
 					$defaultgroupid = $thisacctid;
 				}
+				echo "----------------------------------------------<br>\n";
 			}
 		}
 
-		if(isset($_POST['users']))
+		if ( array_key_exists( 'users', $_POST ) && is_array( $_POST[ 'users' ] ) )
 		{
-			$accounts = CreateObject('phpgwapi.accounts');
-			copyobj($GLOBALS['phpgw_setup']->db,$accounts->db);
+			$accounts = CreateObject( 'phpgwapi.accounts' );
+			copyobj( $GLOBALS[ 'phpgw_setup' ] -> db, $accounts -> db );
+
 			$users_process = 0;
 			$new_uidnumber = 12011;
-			while(list($key,$id) = each($_POST['users']))
+
+			//error_log( print_r( $_POST[ 'users' ], true ), 3, '/tmp/log' );
+			//error_log( print_r( $config, true ), 3, '/tmp/log' );
+
+			foreach ( $_POST[ 'users' ] as $user_id )
 			{
 				$id_exist = 0;
-				$thisacctid  = $account_info[$id]['uidnumber'][0];
-				$thisacctlid = $account_info[$id]['uid'][0];
-				$thisdn      = $account_info[$id]['dn'];
-				echo $thisdn . "<BR>\n";
-				echo "Updating USERID : ".$thisacctlid."<br>\n";
+				$thisacctid  = $account_info[ $user_id ][ 'uidnumber' ][ 0 ];
+				$thisacctlid = $account_info[ $user_id ][ 'uid' ][ 0 ];
+				$thisdn      = $account_info[ $user_id ][ 'dn'];
+
+				echo "{$thisdn}<br>\nUpdating USERID : {$thisacctlid}<br>\n";
 				
-				/* Do some checks before we try to import the data. */
-				if(!empty($thisacctid) && !empty($thisacctlid))
+				// Do some checks before we try to import the data.
+				if ( !empty($thisacctid) && !empty($thisacctlid))
 				{
 					$users_process++;
 
-					$accounts->account_id = (int)$thisacctid;
+					//$res = ldap_search( $ldap, $config[ 'ldap_group_context' ], 'uid=' . $thisacctlid, array( 'uid', 'objectClass' ) );
+					//$entry = ldap_get_entries( $ldap, $res );
+
+					//error_log( print_r( $entry, true ), 3, '/tmp/log' );
+
+					//for ( $entry = ldap_first_entry( $ldap, $res ); $entry !== false, $arr = ldap_get_attributes( $ldap, $entry ); $entry = ldap_next_entry( $ldap, $entry ) )
+					//{
+					//	error_log( print_r( $arr, true ), 3, '/tmp/log' );
+					//}
+					//error_log( "---------------------------------------------------\n", 3, '/tmp/log' );
+
+					//error_log( print_r( $account_info[ $user_id ], true ), 3, '/tmp/log' );
+					//error_log( "---------------------------------------------------\n", 3, '/tmp/log' );
+
+					$add = array( );
+					$objectClass = array( );
+
+					if ( ! in_array( 'qmailUser', $account_info[ $user_id ][ 'objectclass' ] ) )
+						$objectclass[ ] = 'qmailUser';
+
+					if ( ! in_array( 'phpgwAccount', $account_info[ $user_id ][ 'objectclass' ] ) )
+					{
+						$objectclass[ ] = 'phpgwAccount';
+						$add[ 'phpgwAccountExpires' ] = array( '-1' );
+						$add[ 'phpgwAccountStatus' ] = array( 'A' );
+						$add[ 'phpgwAccountType' ] = array( 'u' );
+						$add[ 'phpgwLastPasswdChange' ] = array( '1290632486' );
+					}
+
+					if ( count( $objectclass ) )
+						$add[ 'objectclass' ] = $objectclass;
+
+					if ( count( $add ) )
+						ldap_mod_add( $ldap, $thisdn, $add );
+
+					$accounts -> account_id = ( int ) $thisacctid;
 					
-					/* Insert default acls for this user. */
-					$acl->account_id = (int)$thisacctid;
-					$acl->read_repository();
+					// Insert default acls for this user.
+					$acl -> account_id = ( int ) $thisacctid;
+					$acl -> read_repository( );
 	
 					// Now add the acl to let them change their password
-					$acl->delete('preferences','changepassword',1);
-					$acl->add('preferences','changepassword',1);
+					$acl -> delete( 'preferences', 'changepassword', 1 );
+					$acl -> add( 'preferences', 'changepassword', 1 );
 
 					// Add user to a default group, previous created
-					$acl->add('phpgw_group','12007',1);
-
-					echo "Adding in ACL BD: ".$thisacctid."<br><br>\n";
-					/* Save these new acls. */
-					$acl->save_repository();
+					$acl -> add( 'phpgw_group', '12007', 1 );
+
+					echo "Adding in ACL BD: {$thisacctid}<br><br>\n";
+
+					// Save these new acls.
+					$acl -> save_repository( );
 						
 					$new_uidnumber++;
@@ -289,19 +329,16 @@
 			}
 		}
-		$setup_complete = True;
-	}
-
-	if(isset($_GET['error']))
-	{
-		/* echo '<br><center><b>Error:</b> '.$error.'</center>'; */
-		$GLOBALS['phpgw_setup']->html->show_alert_msg('Error',$_GET['error']);
-	}
-
-	if($setup_complete)
-	{
-		echo '<br><center>'.lang('Modifications have been completed!').' '.lang('Click <a href="index.php">here</a> to return to setup.').'<br><center>';
-		$GLOBALS['phpgw_setup']->html->show_footer();
+
+		printf( "<br><center>%s %s<br></center>",
+			lang( 'Modifications have been completed!' ),
+			lang( 'Click <a href="index.php">here</a> to return to setup.' )
+		);
+
+		$GLOBALS['phpgw_setup' ] -> html -> show_footer( );
 		exit;
 	}
+
+	if ( array_key_exists( 'error', $_GET ) )
+		$GLOBALS[ 'phpgw_setup' ] -> html -> show_alert_msg( 'Error', $_GET[ 'error' ] );
 
 	$setup_tpl->set_block('ldap','header','header');
@@ -313,22 +350,13 @@
 	$setup_tpl->set_block('ldap','footer','footer');
 
-	$user_list = '';
-	while(list($key,$account) = @each($account_info))
-	{
-		$user_list .= '<option value="' . $account['uidnumber'][0] . '">' . utf8_decode($account['cn'][0]) . ' (' . $account['uid'][0] . ')</option>';
-	}
-
-	$admin_list = '';
-	@reset($account_info);
-	while (list($key,$account) = @each($account_info))
-	{
-		$admin_list .= '<option value="' . $account['uidnumber'][0] . '">' . utf8_decode($account['cn'][0]) . ' (' . $account['uid'][0] . ')</option>';
-	}
+	$user_list = array( );
+	while ( list( $key, $account ) = each( $account_info ) )
+		$user_list[ ] = '<option value="' . $account[ 'dn' ] . '">' . utf8_decode( $account[ 'cn' ][ 0 ] ) . " ({$account[ 'uid' ][ 0 ]})</option>";
+
+	$user_list = $admin_list = implode( '', $user_list );
 
 	$group_list = '';
-	while(list($key,$group) = @each($group_info))
-	{
-		$group_list .= '<option value="' . $group['gidnumber'][0] . '">' . utf8_decode($group['cn'][0])  . '</option>';
-	}
+	while( list( $key, $group ) = each( $group_info ) )
+		$group_list .= '<option value="' . $group[ 'dn' ] . '">' . utf8_decode( $group[ 'cn' ][ 0 ] )  . '</option>';
 
 	$app_list = '';
