import.php

Ritesh Sutaria, 2010-09-06 12:49

Download (2.11 KB)

 
1
<?php
2

    
3
        //Postgres Variables
4
        
5
        $pg_sqldb_host = "pghost";
6
        $pg_sqldb_user = "pguser";
7
        $pg_sqldb_pass = "pgpass";
8
        $pg_sqldb_name = "redmine";
9

    
10
        // LDAP variables
11
        $ldaphost = "ldap.server.com";  // your ldap servers
12
        $ldapport = 389;  // your ldap server's port number
13

    
14
        // Connecting to LDAP
15
        $ldapconn = ldap_connect($ldaphost, $ldapport) or die("Could not connect to $ldaphost");
16

    
17
        ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
18
        ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
19

    
20
        $login = ldap_bind( $ldapconn, "ldapuser", "ldappass" );
21

    
22
        $attributes = array("uid", "displayname", "mail");
23
        $filter = "(uid=*)";
24

    
25
        $result = ldap_search($ldapconn, $search_base, $filter, $attributes) or die("error in query") ;
26

    
27
        $data = ldap_get_entries($ldapconn, $result);
28

    
29
        $redmine_link = pg_connect("host=$pg_sqldb_host port=5432 dbname=$pg_sqldb_name user=$pg_sqldb_user password=$pg_sqldb_pass") or die("Could not connect to Redmine: " . pg_last_error());
30

    
31

    
32
        $query = "select MAX(id)+1 from users";
33
        $result = pg_query($redmine_link, $query);
34
        $nextid = pg_fetch_result($result, 0, 0);
35

    
36
        for ($i=0; $i<=$data["count"];$i++) {
37

    
38
                $query = "select id from users where login='" . $data[$i]["uid"][0] . "'";
39
                $result = pg_query($redmine_link, $query);
40

    
41
                $rows = pg_num_rows($result);
42
                
43
                if($rows == 0 )
44
                {
45
                        $name = split(" " , $data[$i]["displayname"][0]);
46

    
47
                        $query = "INSERT INTO users (id, login, firstname, lastname, mail,        mail_notification, admin, status, language, auth_source_id, created_on, type)
48
        VALUES($nextid,'" . $data[$i]["uid"][0] . "','" . $name[0] . "','" . $name[1] . "','" . $data[$i]["mail"][0] . "',false,false,1,'en',$auth_source_id,CURRENT_TIMESTAMP,'User')";
49

    
50
                        $result = pg_query($redmine_link, $query);
51
                        $rows = pg_affected_rows($result);
52

    
53
                        //user doesn't exists add user
54
                        echo ("User " . $data[$i]["uid"][0] . " added successfully<br>");
55
                        $nextid++;
56
                }
57
                else
58
                {
59
                        echo ("User " . $data[$i]["uid"][0] . " already exists<br>");
60
                }
61
        }
62

    
63
        ldap_unbind($login);
64
        ldap_close($ldapconn);
65

    
66
        print("Users imported successfully <br>");
67

    
68
?>