PageRenderTime 22ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/accounts/ldapadmin/htdocs/ldif_import.php

https://github.com/azeckoski/az-php-sandbox
PHP | 202 lines | 140 code | 38 blank | 24 comment | 23 complexity | 017496bc8422c29d2824a3df8c57ae7c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. // $Header: /cvsroot/phpldapadmin/phpldapadmin/htdocs/ldif_import.php,v 1.33.2.3 2005/12/10 06:55:52 wurley Exp $
  3. /**
  4. * Imports an LDIF file to the specified server_id.
  5. *
  6. * Variables that come in as POST vars:
  7. * - ldif_file (as an uploaded file)
  8. * - server_id
  9. *
  10. * @package phpLDAPadmin
  11. */
  12. /**
  13. */
  14. require './common.php';
  15. if (! $ldapserver->haveAuthInfo())
  16. pla_error(_('Not enough information to login to server. Please check your configuration.'));
  17. $continuous_mode = isset($_POST['continuous_mode']) ? 1 : 0;
  18. if (isset($_REQUEST['ldif']) && trim($_REQUEST['ldif'])) {
  19. $textarealdif = $_REQUEST['ldif'];
  20. $remote_file = 'STDIN';
  21. $file_len = strlen($textarealdif);
  22. } elseif (isset($_FILES['ldif_file'])) {
  23. $file = $_FILES['ldif_file']['tmp_name'];
  24. $remote_file = $_FILES['ldif_file']['name'];
  25. $file_len = $_FILES['ldif_file']['size'];
  26. is_array($_FILES['ldif_file']) or pla_error(_('Missing uploaded file.'));
  27. file_exists($file) or pla_error(_('No LDIF file specified. Please try again.'));
  28. $file_len > 0 or pla_error(_('Uploaded LDIF file is empty.'));
  29. } else {
  30. pla_error(_('You must either upload a file or provide an LDIF in the text box.'));
  31. }
  32. include './header.php';
  33. echo '<body>';
  34. printf('<h3 class="title">%s</h3>',_('Import LDIF File'));
  35. printf('<h3 class="subtitle">%s: <b>%s</b> %s: <b>%s (%s %s)</b></h3>',
  36. _('Server'),htmlspecialchars($ldapserver->name),
  37. _('File'),htmlspecialchars($remote_file),number_format($file_len),_('bytes'));
  38. echo '<br /><br />';
  39. require LIBDIR.'ldif_functions.php';
  40. @set_time_limit(0);
  41. # String associated to the operation on the ldap server
  42. $actionString = array();
  43. $actionString['add'] = _('Adding...');
  44. $actionString['delete'] = _('Deleting...');
  45. $actionString['modrdn'] = _('Renaming...');
  46. $actionString['moddn'] = _('Renaming...');
  47. $actionString['modify'] = _('Modifying...');
  48. # String associated with error
  49. $actionErrorMsg =array();
  50. $actionErrorMsg['add'] = _('Could not add object:');
  51. $actionErrorMsg['delete']= _('Could not delete object:');
  52. $actionErrorMsg['modrdn']= _('Could not rename object:');
  53. $actionErrorMsg['moddn']= _('Could not rename object:');
  54. $actionErrorMsg['modify']= _('Could not modify object:');
  55. # instantiate the reader
  56. if (isset($textarealdif))
  57. $ldifReader = new LdifReaderStdIn($textarealdif,$continuous_mode);
  58. else
  59. $ldifReader = new LdifReader($file,$continuous_mode);
  60. # instantiate the writer
  61. $ldapWriter = new LdapWriter($ldapserver);
  62. # if ldif file has no version number, just display a warning
  63. if (!$ldifReader->hasVersionNumber())
  64. display_warning($ldifReader->getWarningMessage());
  65. $i=0;
  66. # if .. else not mandatory but should be easier to maintain
  67. if ($continuous_mode) {
  68. while ($ldifReader->readEntry()) {
  69. $i++;
  70. # get the entry.
  71. $currentEntry = $ldifReader->fetchEntryObject();
  72. $edit_href = sprintf('template_engine.php?server_id=%s&amp;dn=%s',$ldapserver->server_id,
  73. rawurlencode($currentEntry->dn));
  74. $changeType = $currentEntry->getChangeType();
  75. printf('<small>%s <a href="%s">%s</a>',$actionString[$changeType],$edit_href,$entry->dn);
  76. if ($ldifReader->hasRaisedException()) {
  77. printf(' <span style="color:red;">%s</span></small><br />',_('Failed'));
  78. $exception = $ldifReader->getLdapLdifReaderException();
  79. printf(' <small><span style="color:red;">%s: %s</span></small><br />',
  80. _('Line Number'),$exception->lineNumber);
  81. printf(' <small><span style="color:red;">%s: %s</span></small><br />',
  82. _('Line'),$exception->currentLine);
  83. printf(' <small><span style="color:red;">%s: %s</span></small><br />',
  84. _('Description'),$exception->message);
  85. } else {
  86. if ($ldapWriter->ldapModify($currentEntry))
  87. printf(' <span style="color:green;">%s</span></small><br />',_('Success'));
  88. else {
  89. printf('<span style="color:red;">%s</span></small><br />',_('Failed'));
  90. printf('<small><span style="color:red;">%s: %s</span></small><br />',
  91. _('Error code'),$ldapserver->errno());
  92. printf('<small><span style="color:red;">%s: %s</span></small><br />',
  93. _('Description'),$ldapserver->error());
  94. }
  95. }
  96. if ($i % 5 == 0)
  97. flush();
  98. } # end while
  99. } else {
  100. # while we have a valid entry,
  101. while ($entry = $ldifReader->readEntry()) {
  102. $i++;
  103. $edit_href = sprintf('template_engine.php?server_id=%s&amp;dn=%s',$ldapserver->server_id,
  104. rawurlencode($entry->dn));
  105. $changeType = $entry->getChangeType();
  106. printf('<small>%s <a href="%s">%s</a>',$actionString[$changeType],$edit_href,$entry->dn);
  107. if ($ldapWriter->ldapModify($entry)) {
  108. printf(' <span style="color:green;">%s</span></small><br />',_('Success'));
  109. if ($i % 5 == 0)
  110. flush();
  111. } else {
  112. printf(' <span style="color:red;">%s</span></small><br /><br />',_('Failed'));
  113. reload_left_frame();
  114. pla_error($actionErrorMsg[$changeType].' '.htmlspecialchars($entry->dn),
  115. $ldapserver->error(),$ldapserver->errno());
  116. }
  117. }
  118. # if any errors occurs during reading file ,"catch" the exception and display it here.
  119. if ($ldifReader->hasRaisedException()) {
  120. # get the entry which raise the exception,quick hack here
  121. $currentEntry = $ldifReader->fetchEntryObject();
  122. if ($currentEntry->dn != '') {
  123. printf('<small>%s %s <span style="color:red;">%s</span></small><br />',
  124. $actionString[$currentEntry->getChangeType()],$currentEntry->dn,_('Failed'));
  125. }
  126. # get the exception wich was raised
  127. $exception = $ldifReader->getLdapLdifReaderException();
  128. echo '<br /><br />';
  129. display_pla_parse_error($exception,$currentEntry);
  130. }
  131. }
  132. # close the file
  133. $ldifReader->done();
  134. reload_left_frame();
  135. function reload_left_frame(){
  136. echo '<script type="text/javascript" language="javascript">parent.left_frame.location.reload();</script>';
  137. }
  138. function display_error_message($error_message){
  139. printf('<div style="color:red;"><small>%s</small></div>',$error_message);
  140. }
  141. function display_warning($warning){
  142. printf('<div style="color:orange"><small>%s</small></div>',$warning);
  143. }
  144. function display_pla_parse_error($exception,$faultyEntry){
  145. global $actionErrorMsg;
  146. $errorMessage = $actionErrorMsg[$faultyEntry->getChangeType()];
  147. echo '<center>';
  148. echo '<table class="error"><tr><td class="img"><img src="images/warning.png" /></td>';
  149. echo '<td>';
  150. printf('<center><h2>%s</h2></center>',_('LDIF Parse Error'));
  151. echo '<br />';
  152. printf('%s %s',$errorMessage,$faultyEntry->dn);
  153. printf('<p><b>%s</b>: %s</p>',_('Description'),$exception->message);
  154. printf('<p><b>%s</b>: %s</p>',_('Line'),$exception->currentLine);
  155. printf('<p><b>%s</b>: %s</p>',_('Line Number'),$exception->lineNumber);
  156. echo '<br />';
  157. printf('<p><center><small>%s %s</small></center></p>',
  158. _('Is this a phpLDAPadmin bug?'),sprintf(_('If so, please <a href="%s">report it</a>.'),get_href('add_bug')));
  159. echo '</td>';
  160. echo '</tr>';
  161. echo '<center>';
  162. }
  163. echo '</body></html>';
  164. ?>