#!/usr/bin/php
<?php
	# Mantis - a php based bugtracking system
	# Copyright (C) 2000 - 2002  Kenzaburo Ito - kenito@300baud.org
	# Copyright (C) 2002 - 2004  Mantis Team   - mantisbt-dev@lists.sourceforge.net
	# This program is distributed under the terms and conditions of the GPL
	# See the README and LICENSE files for details

	# --------------------------------------------------------
	# $Id: signup.php,v 1.38 2004/09/28 13:57:37 thraxisp Exp $
	# --------------------------------------------------------

	require_once( '../epia/core.php' );

	$t_core_path = config_get( 'core_path' );

	require_once( $t_core_path.'user_api.php' );
	require_once( $t_core_path.'ldap_api.php' );
	

	$t_ldap_root_dn		= config_get( 'ldap_root_dn' );
	$t_ldap_uid_field	= config_get( 'ldap_uid_field', 'uid' ) ;
	
	$t_ldap_organizations[0] = 'OU=Users;OU=ETR;OU=Companies;DC=iserv,DC=vads,DC=cc';
	$t_ldap_organizations[1] = 'OU=Externals;OU=ETR;OU=Companies;DC=iserv,DC=vads,DC=cc';
	
	foreach ($t_ldap_organizations as $t_ldap_org) {
	    $t_search_filter 	= "(CN=*)";
	    $t_search_attrs  	= array( 'sAMAccountName' , 'dn', 'mail', 'cn' );
	    $t_ds            	= ldap_connect_bind();

	    # Search for the user id
	    $t_sr	= ldap_search( $t_ds, $t_ldap_org, $t_search_filter, $t_search_attrs );
	    $t_info	= ldap_get_entries( $t_ds, $t_sr );

	    if ( $t_info ) {
		    # Try to authenticate to each until we get a match
		    for ( $i = 0 ; $i < $t_info['count'] ; $i++ ) {
			    $t_user = $t_info[$i]['samaccountname'][0];
			    $t_mail = $t_info[$i]['mail'][0];
			    $t_real_user = $t_info[$i]['cn'][0];
			    
			    if ( user_is_name_unique ($t_user) ) {
				# User is not known
				if ( user_create ( $t_user , '$5%223%2$3&' , $t_mail , null , false , true , $t_real_user ) ) {
				    echo "Created User $t_real_user [$t_user] : $t_mail\n";
				} else {
				    echo "ERROR: Could not create user $t_real_user [$t_user] : $t_mail";
				} 
			    }
		    }
	    }

	    ldap_free_result( $t_sr );
	    ldap_unbind( $t_ds );
	}
?>