2
// $Header: /cvsroot/phpldapadmin/phpldapadmin/htdocs/ldif_import.php,v 1.35.2.4 2008/12/12 12:20:22 wurley Exp $
5
* Imports an LDIF file to the specified server_id.
7
* Variables that come in as POST vars:
8
* - ldif_file (as an uploaded file)
11
* @package phpLDAPadmin
16
require './common.php';
18
if (! $_SESSION[APPCONFIG]->isCommandAvailable('import'))
19
error(sprintf('%s%s %s',_('This operation is not permitted by the configuration'),_(':'),_('import')),'error','index.php');
22
$entry['continuous_mode'] = get_request('continuous_mode') ? true : false;
23
$entry['ldif'] = get_request('ldif');
26
$entry['remote_file'] = 'STDIN';
27
$entry['size'] = strlen($entry['ldif']);
29
} elseif (isset($_FILES['ldif_file'])) {
30
$file = $_FILES['ldif_file']['tmp_name'];
31
$entry['remote_file'] = $_FILES['ldif_file']['name'];
32
$entry['size'] = $_FILES['ldif_file']['size'];
34
if (! is_array($_FILES['ldif_file'])) {
35
error(_('Missing uploaded file.'),'error');
38
if (! file_exists($file)) {
39
error(_('No LDIF file specified. Please try again.'),'error');
42
if ($entry['size'] <= 0) {
43
error(_('Uploaded LDIF file is empty.'),'error');
48
error(_('You must either upload a file or provide an LDIF in the text box.'),'error');
52
printf('<h3 class="title">%s</h3>',_('Import LDIF File'));
53
printf('<h3 class="subtitle">%s: <b>%s</b> %s: <b>%s (%s %s)</b></h3>',
54
_('Server'),htmlspecialchars($ldapserver->name),
55
_('File'),htmlspecialchars($entry['remote_file']),number_format($entry['size']),_('bytes'));
58
require LIBDIR.'ldif_functions.php';
61
# String associated to the operation on the ldap server
62
$actionString = array();
63
$actionString['add'] = _('Adding...');
64
$actionString['delete'] = _('Deleting...');
65
$actionString['modrdn'] = _('Renaming...');
66
$actionString['moddn'] = _('Renaming...');
67
$actionString['modify'] = _('Modifying...');
69
# String associated with error
70
$actionErrorMsg =array();
71
$actionErrorMsg['add'] = _('Could not add object:');
72
$actionErrorMsg['delete']= _('Could not delete object:');
73
$actionErrorMsg['modrdn']= _('Could not rename object:');
74
$actionErrorMsg['moddn']= _('Could not rename object:');
75
$actionErrorMsg['modify']= _('Could not modify object:');
77
# instantiate the reader
78
if (isset($entry['ldif']))
79
$ldifReader = new LdifReaderStdIn($entry['ldif'],$entry['continuous_mode']);
81
$ldifReader = new LdifReader($file,$entry['continuous_mode']);
83
# instantiate the writer
84
$ldapWriter = new LdapWriter($ldapserver);
86
# if ldif file has no version number, just display a warning
87
if (!$ldifReader->hasVersionNumber())
88
display_warning($ldifReader->getWarningMessage());
91
# if .. else not mandatory but should be easier to maintain
92
if ($entry['continuous_mode']) {
93
while ($ldifReader->readEntry()) {
97
$currentEntry = $ldifReader->fetchEntryObject();
98
$edit_href = sprintf('cmd.php?cmd=template_engine&server_id=%s&dn=%s',$ldapserver->server_id,
99
rawurlencode($currentEntry->dn));
100
$changeType = $currentEntry->getChangeType();
101
printf('<small>%s <a href="%s">%s</a>',$actionString[$changeType],$edit_href,$currentEntry->dn);
103
if ($ldifReader->hasRaisedException()) {
104
printf(' <span style="color:red;">%s</span></small><br />',_('Failed'));
105
$exception = $ldifReader->getLdapLdifReaderException();
106
printf(' <small><span style="color:red;">%s: %s</span></small><br />',
107
_('Line Number'),$exception->lineNumber);
108
printf(' <small><span style="color:red;">%s: %s</span></small><br />',
109
_('Line'),$exception->currentLine);
110
printf(' <small><span style="color:red;">%s: %s</span></small><br />',
111
_('Description'),$exception->message);
114
if ($ldapWriter->ldapModify($currentEntry))
115
printf(' <span style="color:green;">%s</span></small><br />',_('Success'));
117
printf(' <span style="color:red;">%s</span></small><br />',_('Failed'));
118
printf(' <small><span style="color:red;">%s: %s</span></small><br />',
119
_('Error code'),$ldapserver->errno());
120
printf(' <small><span style="color:red;">%s: %s</span></small><br />',
121
_('Description'),$ldapserver->error());
127
# while we have a valid entry,
128
while ($entry = $ldifReader->readEntry()) {
131
$edit_href = sprintf('cmd.php?cmd=template_engine&server_id=%s&dn=%s',$ldapserver->server_id,
132
rawurlencode($entry->dn));
133
$changeType = $entry->getChangeType();
134
printf('<small>%s <a href="%s">%s</a>',$actionString[$changeType],$edit_href,$entry->dn);
136
if ($ldapWriter->ldapModify($entry)) {
137
printf(' <span style="color:green;">%s</span></small><br />',_('Success'));
140
printf(' <span style="color:red;">%s</span></small><br /><br />',_('Failed'));
141
$ldap_err_no = ('0x'.str_pad(dechex($ldapserver->errno()),2,0,STR_PAD_LEFT));
142
$verbose_error = pla_verbose_error($ldap_err_no);
144
$errormsg = sprintf('%s <b>%s</b>',$actionErrorMsg[$changeType],htmlspecialchars($entry->dn));
145
$errormsg .= sprintf('<br />%s: <b>%s</b>',_('LDAP said'),$verbose_error['title']);
146
$errormsg .= sprintf('<br />%s',$verbose_error['desc']);
147
system_message(array(
148
'title'=>_('LDIF text import'),
156
# if any errors occurs during reading file ,"catch" the exception and display it here.
157
if ($ldifReader->hasRaisedException()) {
158
# get the entry which raise the exception,quick hack here
159
$currentEntry = $ldifReader->fetchEntryObject();
161
if ($currentEntry->dn != '') {
162
printf('<small>%s %s <span style="color:red;">%s</span></small><br />',
163
$actionString[$currentEntry->getChangeType()],$currentEntry->dn,_('Failed'));
166
# get the exception wich was raised
167
$exception = $ldifReader->getLdapLdifReaderException();
169
display_pla_parse_error($exception,$currentEntry);
176
function display_warning($warning){
177
printf('<div style="color:orange"><small>%s</small></div>',$warning);
180
function display_pla_parse_error($exception,$faultyEntry) {
181
global $actionErrorMsg;
183
$errorMessage = $actionErrorMsg[$faultyEntry->getChangeType()];
186
printf('<table class="error"><tr><td class="img"><img src="%s/warning.png" /></td>',IMGDIR);
188
printf('<center><h2>%s</h2></center>',_('LDIF Parse Error'));
190
printf('%s %s',$errorMessage,$faultyEntry->dn);
191
printf('<p><b>%s</b>: %s</p>',_('Description'),$exception->message);
192
printf('<p><b>%s</b>: %s</p>',_('Line'),$exception->currentLine);
193
printf('<p><b>%s</b>: %s</p>',_('Line Number'),$exception->lineNumber);
195
printf('<p><center><small>%s %s</small></center></p>',
196
_('Is this a phpLDAPadmin bug?'),sprintf(_('If so, please <a href="%s">report it</a>.'),get_href('add_bug')));