File: ldif_import.php

package info (click to toggle)
phpldapadmin 0.9.5-3sarge3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,052 kB
  • ctags: 2,526
  • sloc: php: 21,258; sh: 262; makefile: 132; xml: 42
file content (204 lines) | stat: -rw-r--r-- 6,749 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
// $Header: /cvsroot/phpldapadmin/phpldapadmin/ldif_import.php,v 1.27 2004/08/15 17:39:20 uugdave Exp $
 

/*
 * ldif_import.php
 * Imports an LDIF file to the specified server_id.
 *
 * Variables that come in as POST vars:
 *  - ldif_file (as an uploaded file)
 *  - server_id
 */

require './common.php';

$debug = true;

$server_id = $_POST['server_id'];
$continuous_mode =  isset( $_POST['continuous_mode'] ) ?1:0;
$server_name = $servers[$server_id]['name'];
$file = $_FILES['ldif_file']['tmp_name'];
$remote_file = $_FILES['ldif_file']['name'];
$file_len = $_FILES['ldif_file']['size'];

is_array( $_FILES['ldif_file'] ) or pla_error( $lang['missing_uploaded_file'] );
file_exists( $file ) or pla_error( $lang['no_ldif_file_specified'] );
$file_len > 0 or pla_error( $lang['ldif_file_empty'] );
check_server_id( $server_id ) or pla_error( $lang['bad_server_id'] );
have_auth_info( $server_id ) or pla_error( $lang['not_enough_login_info'] );

include './header.php'; ?>

<body>

<h3 class="title"><?php echo $lang['import_ldif_file_title']; ?></h3>
<h3 class="subtitle">
	<?php echo $lang['server']; ?>: <b><?php echo htmlspecialchars( $server_name ); ?></b>
	<?php echo $lang['file']; ?>: <b><?php echo htmlspecialchars( $remote_file ); ?>
	(<?php echo sprintf( $lang['number_bytes'], number_format( $file_len ) ); ?>)</b>
</h3>

<br />
<br />

<?php 
include("ldif_functions.php");	
@set_time_limit( 0 );

// String associated to the operation on the ldap server
$actionString = array();
$actionString['add'] = $lang['add_action'];
$actionString['delete'] = $lang['delete_action'];
$actionString['modrdn'] = $lang['rename_action'];
$actionString['moddn'] = $lang['rename_action'];
$actionString['modify'] = $lang['modify_action'];


// String associated with error
$actionErrorMsg =array();
$actionErrorMsg['add'] = $lang['ldif_could_not_add_object'];
$actionErrorMsg['delete']= $lang['ldif_could_not_delete_object'];
$actionErrorMsg['modrdn']= $lang['ldif_could_not_rename_object'];
$actionErrorMsg['moddn']= $lang['ldif_could_not_rename_object'];
$actionErrorMsg['modify']= $lang['ldif_could_not_modify_object'];

// get the connection
$ds = pla_ldap_connect( $server_id );
pla_ldap_connection_is_error( $ds );

//instantiate the reader
$ldifReader = new LdifReader($file,$continuous_mode);

//instantiate the writer
$ldapWriter = new LdapWriter($ds);

// if ldif file has no version number, just display a warning
if(!$ldifReader->hasVersionNumber()){
  display_warning($ldifReader->getWarningMessage());
}
$i=0;
// if .. else not mandatory but should be easier to maintain
if( $continuous_mode ){
    while( $ldifReader->readEntry() ){
         $i++;  
       // get the entry. 
       $currentEntry = $ldifReader->getCurrentEntry();
       $changeType = $currentEntry->getChangeType();
       echo "<small>".$actionString[$changeType]." ".$currentEntry->dn;

       if($ldifReader->hasRaisedException()){
          echo " <span style=\"color:red;\">".$lang['failed']."</span></small><br>";
    $exception = $ldifReader->getLdapLdifReaderException();
             echo "<small><span style=\"color:red;\">".$lang['ldif_line_number'].": ".$exception->lineNumber."</span></small><br />";
echo "<small><span style=\"color:red;\">".$lang['ldif_line'].": ".$exception->currentLine."</span></small><br />";   
echo "<small><span style=\"color:red;\">".$lang['desc'].": ".$exception->message."</span></small><br />";
}
       else{
         if($ldapWriter->ldapModify($currentEntry))
             echo " <span style=\"color:green;\">".$lang['success']."</span></small><br>";
          else{
             echo " <span style=\"color:red;\">".$lang['failed']."</span></small><br>";
             echo "<small><span style=\"color:red;\">Error Code: ".ldap_errno($ds)."</span></small><br />";
             echo "<small><span style=\"color:red;\">".$lang['desc'].": ".ldap_error($ds)."</span></small><br />";
          }
      }
    if( 0 == $i % 5 )
      flush();
   }// end while
}
else{
//while we have a valid entry, 
while($entry = $ldifReader->readEntry()){
  $i++;
  $changeType = $entry->getChangeType();

  echo "<small>".$actionString[$changeType]." ".$entry->dn;
  if($ldapWriter->ldapModify($entry)){
    echo " <span style=\"color:green;\">".$lang['success']."</span></small><br>";
    if( 0 == $i % 5 )
    flush();
  }
  else{
    echo " <span style=\"color:red;\">".$lang['failed']."</span></small><br><br>";
    reload_left_frame();
    pla_error( $actionErrorMsg[$changeType]. " " . htmlspecialchars( $entry->dn  ), ldap_error( $ds ), ldap_errno( $ds ) );
  }
}

// if any errors occurs during reading file ,"catch" the exception and display it here.
  if($ldifReader->hasRaisedException()){
    //get the entry which raise the exception,quick hack here 
    $currentEntry = $ldifReader->getCurrentEntry();

    if($currentEntry->dn !=""){
      echo "<small>".$actionString[$currentEntry->getChangeType()]." ".$currentEntry->dn." <span style=\"color:red;\">".$lang['failed']."</span></small><br>";
    }
    //get the exception wich was raised
    $exception = $ldifReader->getLdapLdifReaderException();
echo "<br />";
echo "<br />";
display_pla_parse_error($exception,$currentEntry);
  }
}
// close the file
$ldifReader->done();

//close the ldap connection
$ldapWriter->ldapClose();

reload_left_frame();


function reload_left_frame(){
  global $server_id;
  
  echo "<script>\r\n";
  echo "parent.left_frame.document.location='refresh.php?server_id=".$server_id."';\r\n";
  echo "</script>\r\n";
}

function display_error_message($error_message){
  echo "<div style=\"color:red;\"><small>".$error_message."</small></div>";
}
function display_warning($warning){
  echo "<div style=\"color:orange\"><small>".$warning."</small></div>";
}

function display_pla_parse_error($exception,$faultyEntry){
  global $lang;
  global $actionErrorMsg;
  $errorMessage =  $actionErrorMsg[$faultyEntry->getChangeType()];

  print("<center>");
  print("<table class=\"error\"><tr><td class=\"img\"><img src=\"images/warning.png\" /></td>");
  print("<td><center><h2>".$lang['ldif_parse_error']."</h2></center>");
  print("<br />");
  print($errorMessage." ". $faultyEntry->dn);
  print("<p>");
  print("<b>".$lang['desc']."</b>: ".$exception->message);
  print("</p>");
  print("<p>");
  print("<b>".$lang['ldif_line']."</b>: ".$exception->currentLine);
  print("</p>");
  print("<p>");
  print("<b>".$lang['ldif_line_number']."</b>: ".$exception->lineNumber);
  print("</p>");
  print("<br />");
  print("<p>\r\n");
  print("<center>");
  print("<small>");
  print(sprintf($lang['ferror_submit_bug'] , get_href( 'add_bug' )));

 print("</small></center></p>");
 print("<td>");
 print("</tr>");
 print("<center>");

}

?>
</body>
</html>