isCommandAvailable('import')) error(sprintf('%s%s %s',_('This operation is not permitted by the configuration'),_(':'),_('import')),'error','index.php'); $entry = array(); $entry['continuous_mode'] = get_request('continuous_mode') ? true : false; $entry['ldif'] = get_request('ldif'); if ($entry['ldif']) { $entry['remote_file'] = 'STDIN'; $entry['size'] = strlen($entry['ldif']); } elseif (isset($_FILES['ldif_file'])) { $file = $_FILES['ldif_file']['tmp_name']; $entry['remote_file'] = $_FILES['ldif_file']['name']; $entry['size'] = $_FILES['ldif_file']['size']; if (! is_array($_FILES['ldif_file'])) { error(_('Missing uploaded file.'),'error'); return; } if (! file_exists($file)) { error(_('No LDIF file specified. Please try again.'),'error'); return; } if ($entry['size'] <= 0) { error(_('Uploaded LDIF file is empty.'),'error'); return; } } else { error(_('You must either upload a file or provide an LDIF in the text box.'),'error'); return; } printf('

%s

',_('Import LDIF File')); printf('

%s: %s %s: %s (%s %s)

', _('Server'),htmlspecialchars($ldapserver->name), _('File'),htmlspecialchars($entry['remote_file']),number_format($entry['size']),_('bytes')); echo '

'; require LIBDIR.'ldif_functions.php'; @set_time_limit(0); # String associated to the operation on the ldap server $actionString = array(); $actionString['add'] = _('Adding...'); $actionString['delete'] = _('Deleting...'); $actionString['modrdn'] = _('Renaming...'); $actionString['moddn'] = _('Renaming...'); $actionString['modify'] = _('Modifying...'); # String associated with error $actionErrorMsg =array(); $actionErrorMsg['add'] = _('Could not add object:'); $actionErrorMsg['delete']= _('Could not delete object:'); $actionErrorMsg['modrdn']= _('Could not rename object:'); $actionErrorMsg['moddn']= _('Could not rename object:'); $actionErrorMsg['modify']= _('Could not modify object:'); # instantiate the reader if (isset($entry['ldif'])) $ldifReader = new LdifReaderStdIn($entry['ldif'],$entry['continuous_mode']); else $ldifReader = new LdifReader($file,$entry['continuous_mode']); # instantiate the writer $ldapWriter = new LdapWriter($ldapserver); # 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 ($entry['continuous_mode']) { while ($ldifReader->readEntry()) { $i++; # get the entry. $currentEntry = $ldifReader->fetchEntryObject(); $edit_href = sprintf('cmd.php?cmd=template_engine&server_id=%s&dn=%s',$ldapserver->server_id, rawurlencode($currentEntry->dn)); $changeType = $currentEntry->getChangeType(); printf('%s %s',$actionString[$changeType],$edit_href,$currentEntry->dn); if ($ldifReader->hasRaisedException()) { printf(' %s
',_('Failed')); $exception = $ldifReader->getLdapLdifReaderException(); printf(' %s: %s
', _('Line Number'),$exception->lineNumber); printf(' %s: %s
', _('Line'),$exception->currentLine); printf(' %s: %s
', _('Description'),$exception->message); } else { if ($ldapWriter->ldapModify($currentEntry)) printf(' %s
',_('Success')); else { printf(' %s
',_('Failed')); printf(' %s: %s
', _('Error code'),$ldapserver->errno()); printf(' %s: %s
', _('Description'),$ldapserver->error()); } } } # end while } else { # while we have a valid entry, while ($entry = $ldifReader->readEntry()) { $i++; $edit_href = sprintf('cmd.php?cmd=template_engine&server_id=%s&dn=%s',$ldapserver->server_id, rawurlencode($entry->dn)); $changeType = $entry->getChangeType(); printf('%s %s',$actionString[$changeType],$edit_href,$entry->dn); if ($ldapWriter->ldapModify($entry)) { printf(' %s
',_('Success')); } else { printf(' %s

',_('Failed')); $ldap_err_no = ('0x'.str_pad(dechex($ldapserver->errno()),2,0,STR_PAD_LEFT)); $verbose_error = pla_verbose_error($ldap_err_no); $errormsg = sprintf('%s %s',$actionErrorMsg[$changeType],htmlspecialchars($entry->dn)); $errormsg .= sprintf('
%s: %s',_('LDAP said'),$verbose_error['title']); $errormsg .= sprintf('
%s',$verbose_error['desc']); system_message(array( 'title'=>_('LDIF text import'), 'body'=>$errormsg, 'type'=>'warn')); break; } } # 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->fetchEntryObject(); if ($currentEntry->dn != '') { printf('%s %s %s
', $actionString[$currentEntry->getChangeType()],$currentEntry->dn,_('Failed')); } # get the exception wich was raised $exception = $ldifReader->getLdapLdifReaderException(); echo '

'; display_pla_parse_error($exception,$currentEntry); } } # close the file $ldifReader->done(); function display_warning($warning){ printf('
%s
',$warning); } function display_pla_parse_error($exception,$faultyEntry) { global $actionErrorMsg; $errorMessage = $actionErrorMsg[$faultyEntry->getChangeType()]; echo '
'; printf('',IMGDIR); echo ''; echo ''; echo '
'; } ?>
'; printf('

%s

',_('LDIF Parse Error')); echo '
'; printf('%s %s',$errorMessage,$faultyEntry->dn); printf('

%s: %s

',_('Description'),$exception->message); printf('

%s: %s

',_('Line'),$exception->currentLine); printf('

%s: %s

',_('Line Number'),$exception->lineNumber); echo '
'; printf('

%s %s

', _('Is this a phpLDAPadmin bug?'),sprintf(_('If so, please report it.'),get_href('add_bug'))); echo '