~ubuntu-branches/debian/wheezy/phpldapadmin/wheezy

« back to all changes in this revision

Viewing changes to htdocs/ldif_import.php

  • Committer: Bazaar Package Importer
  • Author(s): Fabio Tranchitella
  • Date: 2010-04-10 10:12:22 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20100410101222-3xyuhy4a7usewxla
Tags: 1.2.0.5-1
* New upstream release. (Closes: #571672, #549464)
* debian/po/ru.po: added. (Closes: #536402)
* applied patch to fix lintian warnings. (Closes: #531649)
* Removed debian/patches that have been merged upstream.
* Do not build-depend anymore on dpatch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
// $Header: /cvsroot/phpldapadmin/phpldapadmin/htdocs/ldif_import.php,v 1.35.2.4 2008/12/12 12:20:22 wurley Exp $
3
 
 
4
 
/**
5
 
 * Imports an LDIF file to the specified server_id.
6
 
 *
7
 
 * Variables that come in as POST vars:
8
 
 *  - ldif_file (as an uploaded file)
9
 
 *  - server_id
10
 
 *
11
 
 * @package phpLDAPadmin
12
 
 */
13
 
/**
14
 
 */
15
 
 
16
 
require './common.php';
17
 
 
18
 
if (! $_SESSION[APPCONFIG]->isCommandAvailable('import'))
19
 
        error(sprintf('%s%s %s',_('This operation is not permitted by the configuration'),_(':'),_('import')),'error','index.php');
20
 
 
21
 
$entry = array();
22
 
$entry['continuous_mode'] = get_request('continuous_mode') ? true : false;
23
 
$entry['ldif'] = get_request('ldif');
24
 
 
25
 
if ($entry['ldif']) {
26
 
        $entry['remote_file'] = 'STDIN';
27
 
        $entry['size'] = strlen($entry['ldif']);
28
 
 
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'];
33
 
 
34
 
        if (! is_array($_FILES['ldif_file'])) {
35
 
                error(_('Missing uploaded file.'),'error');
36
 
                return;
37
 
        }
38
 
        if (! file_exists($file)) {
39
 
                error(_('No LDIF file specified. Please try again.'),'error');
40
 
                return;
41
 
        }
42
 
        if ($entry['size'] <= 0) {
43
 
                error(_('Uploaded LDIF file is empty.'),'error');
44
 
                return;
45
 
        }
46
 
 
47
 
} else {
48
 
        error(_('You must either upload a file or provide an LDIF in the text box.'),'error');
49
 
        return;
50
 
}
51
 
 
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'));
56
 
echo '<br /><br />';
57
 
 
58
 
require LIBDIR.'ldif_functions.php';
59
 
@set_time_limit(0);
60
 
 
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...');
68
 
 
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:');
76
 
 
77
 
# instantiate the reader
78
 
if (isset($entry['ldif']))
79
 
        $ldifReader = new LdifReaderStdIn($entry['ldif'],$entry['continuous_mode']);
80
 
else
81
 
        $ldifReader = new LdifReader($file,$entry['continuous_mode']);
82
 
 
83
 
# instantiate the writer
84
 
$ldapWriter = new LdapWriter($ldapserver);
85
 
 
86
 
# if ldif file has no version number, just display a warning
87
 
if (!$ldifReader->hasVersionNumber())
88
 
        display_warning($ldifReader->getWarningMessage());
89
 
 
90
 
$i=0;
91
 
# if .. else not mandatory but should be easier to maintain
92
 
if ($entry['continuous_mode']) {
93
 
        while ($ldifReader->readEntry()) {
94
 
                $i++;
95
 
 
96
 
                # get the entry. 
97
 
                $currentEntry = $ldifReader->fetchEntryObject();
98
 
                $edit_href = sprintf('cmd.php?cmd=template_engine&amp;server_id=%s&amp;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);
102
 
 
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);
112
 
 
113
 
                } else {
114
 
                        if ($ldapWriter->ldapModify($currentEntry))
115
 
                                printf(' <span style="color:green;">%s</span></small><br />',_('Success'));
116
 
                        else {
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());
122
 
                        }
123
 
                }
124
 
        } # end while
125
 
 
126
 
} else {
127
 
        # while we have a valid entry, 
128
 
        while ($entry = $ldifReader->readEntry()) {
129
 
                $i++;
130
 
 
131
 
                $edit_href = sprintf('cmd.php?cmd=template_engine&amp;server_id=%s&amp;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);
135
 
 
136
 
                if ($ldapWriter->ldapModify($entry)) {
137
 
                        printf(' <span style="color:green;">%s</span></small><br />',_('Success'));
138
 
 
139
 
                } else {
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);
143
 
 
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'),
149
 
                                'body'=>$errormsg,
150
 
                                'type'=>'warn'));
151
 
 
152
 
                        break;
153
 
                }
154
 
        }
155
 
 
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();
160
 
 
161
 
                if ($currentEntry->dn != '') {
162
 
                        printf('<small>%s %s <span style="color:red;">%s</span></small><br />',
163
 
                                $actionString[$currentEntry->getChangeType()],$currentEntry->dn,_('Failed'));
164
 
                }
165
 
 
166
 
                # get the exception wich was raised
167
 
                $exception = $ldifReader->getLdapLdifReaderException();
168
 
                echo '<br /><br />';
169
 
                display_pla_parse_error($exception,$currentEntry);
170
 
        }
171
 
}
172
 
 
173
 
# close the file
174
 
$ldifReader->done();
175
 
 
176
 
function display_warning($warning){
177
 
        printf('<div style="color:orange"><small>%s</small></div>',$warning);
178
 
}
179
 
 
180
 
function display_pla_parse_error($exception,$faultyEntry) {
181
 
        global $actionErrorMsg;
182
 
 
183
 
        $errorMessage = $actionErrorMsg[$faultyEntry->getChangeType()];
184
 
 
185
 
        echo '<center>';
186
 
        printf('<table class="error"><tr><td class="img"><img src="%s/warning.png" /></td>',IMGDIR);
187
 
        echo '<td>';
188
 
        printf('<center><h2>%s</h2></center>',_('LDIF Parse Error'));
189
 
        echo '<br />';
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);
194
 
        echo '<br />';
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')));
197
 
 
198
 
        echo '</td>';
199
 
        echo '</tr>';
200
 
        echo '<center>';
201
 
}
202
 
?>