class_ldapmanager.inc 6.9 KB
Newer Older
1 2 3 4
<?php
/*
  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
  Copyright (C) 2003-2010  Cajus Pollmeier
5
  Copyright (C) 2011-2016  FusionDirectory
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
class BaseSelectorOrDnAttribute extends BaseSelectorAttribute
{
  function applyPostValue ()
  {
    global $config;
    if (!$this->disabled && $this->isVisible()) {
      if (isset($_POST[$this->getHtmlId()])) {
        $ldap = $config->get_ldap_link();
        if ($ldap->dn_exists($_POST[$this->getHtmlId()])) {
          $this->setValue($_POST[$this->getHtmlId()]);
          return;
        }
      }
    }
    return parent::applyPostValue();
  }
}
38 39 40

class ldapmanager extends simplePlugin
{
41
  /* Return plugin information for acl handling */
42 43 44
  static function plInfo()
  {
    return array(
45
      'plShortName'   => _('LDIF'),
46
      'plDescription' => _('Export/Import the ldap tree to/from LDIF format'),
47
      'plObjectType'  => array('ldapmanager' => array(
48
        'name'      => _('LDAP Manager'),
49
        'tabClass'  => 'ldiftabs',
50
        'mainAttr'  => FALSE
51
      )),
52

53
      'plProvidedAcls' => parent::generatePlProvidedAcls(static::getAttributesInfo())
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    );
  }

  static function getAttributesInfo ()
  {
    return array(
      'export' => array(
        'name'  => _('Export'),
        'attrs' => array(
          new CompositeAttribute (
            _('DN of a single entry to export as ldif'),
            'single_export',
            array(
              new StringAttribute (
                '', '',
                'single_dn', FALSE
              ),
              new ButtonAttribute (
                '', '',
73 74
                'single_submit',
                _('Export')
75 76 77 78 79 80 81 82
              )
            ),
            '', '%s%s', '',
            _('Export single entry')
          ),
          new StringAttribute (
            _('Filter'), _('Filter to use for selecting objects to export'),
            'export_filter', FALSE,
83 84
            '(objectClass=*)',
            'complete_export'
85 86 87 88 89
          ),
          new CompositeAttribute (
            _('Download a complete snapshot of the running LDAP directory for this base as ldif'),
            'complete_export',
            array(
90
              new BaseSelectorOrDnAttribute ('', '', ''),
91 92
              new ButtonAttribute (
                '', '',
93 94
                'complete_submit',
                _('Export')
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
              )
            ),
            '', '%s%s', '',
            _('Export complete LDIF for')
          )
        )
      ),
      'import' => array(
        'name'  => _('Import LDIF'),
        'attrs' => array(
          new BooleanAttribute (
            _('Overwrite existing entries'), _('Remove fields that are not in the LDIF from the LDAP entries if they were existing.'),
            'overwrite', FALSE,
            FALSE, 'import'
          ),
          new CompositeAttribute (
            _('Import an LDIF file into your LDAP. Remember that FusionDirectory will not check your LDIFs for FusionDirectory conformance.'),
            'import',
            array(
114
              new FileTextAreaAttribute (
115
                '', '',
116
                'import_ldif_file', FALSE
117 118 119
              ),
              new ButtonAttribute (
                '', '',
120
                'import_ldif_submit',
121
                _('Import')
122 123 124 125 126 127 128 129 130 131
              )
            ),
            '', '%s%s', '',
            _('Import LDIF file')
          )
        )
      ),
    );
  }

132
  function __construct ($dn = NULL, $object = NULL, $parent = NULL, $mainTab = FALSE)
133
  {
134
    parent::__construct($dn, $object, $parent, $mainTab);
135 136 137 138 139 140 141 142 143 144 145

    $this->attributesAccess['single_export']->setInLdap(FALSE);
    $this->attributesAccess['single_export']->setLinearRendering(TRUE);
    $this->attributesAccess['complete_export']->setInLdap(FALSE);
    $this->attributesAccess['complete_export']->setLinearRendering(TRUE);
    $this->attributesAccess['import']->setInLdap(FALSE);
    $this->attributesAccess['import']->setLinearRendering(TRUE);
    $this->attributesAccess['overwrite']->setInLdap(FALSE);
    $this->attributesAccess['export_filter']->setInLdap(FALSE);
  }

146
  function handle_single_submit()
147
  {
148 149
    global $config;
    $ldap = $config->get_ldap_link();
150 151
    $dn   = $this->single_export;
    $acl  = $this->attributesAccess['single_export']->getAcl();
152 153
    if (!$ldap->dn_exists($dn)) {
      msg_dialog::display(_('LDAP error'),
154
          sprintf(_('No such object %s!'), $dn),
155 156
          ERROR_DIALOG);
      $dn = '';
157
    }
158
    $this->save_export($dn, $acl, 'base', 'entryExport.ldif');
159 160
  }

161
  function handle_complete_submit()
162
  {
163 164 165
    $acl    = $this->attributesAccess['complete_export']->getAcl();
    $this->save_export($this->complete_export, $acl, 'sub', 'fullExport.ldif');
  }
166

167 168
  function save_export($dn, $acl, $scope, $name)
  {
169
    global $config;
Mortier Benoit's avatar
Mortier Benoit committed
170 171
    // An LDIF export was asked
    if (!empty($dn)) {
172 173 174
      // Check permissions
      if (!$this->acl_is_writeable($acl, $this->acl_skip_write())) {
        msg_dialog::display(_('Permission error'),
175
            sprintf(_('You have no permission to export %s!'), bold($dn)),
176 177
            ERROR_DIALOG);
      } else {
178
        $ldap = $config->get_ldap_link();
179
        $data = $ldap->generateLdif($dn, $this->export_filter, $scope);
180 181 182 183 184 185 186 187 188 189 190
        if ($data === NULL) {
          msg_dialog::display(_('Error'),
              sprintf(_('Failed to generate ldap export, error was "%s"!'), $ldap->get_error()),
              ERROR_DIALOG);
        } else {
          send_binary_content($data, $name);
        }
      }
    }
  }

191
  function handle_import_ldif_submit()
192
  {
193
    global $config;
194 195 196 197
    if (empty($this->import)) {
      msg_dialog::display(_('Warning'), _('Nothing to import, please upload a non-empty file or fill the textarea.'), WARNING_DIALOG);
      return;
    }
198
    $ldap = $config->get_ldap_link();
199
    try {
200 201
      $nb = $ldap->import_complete_ldif($this->import, !$this->overwrite, FALSE);
      msg_dialog::display(_('Success'), sprintf(_('%d entries successfully imported'), $nb), INFO_DIALOG);
202
    } catch (FusionDirectoryException $e) {
203
      msg_dialog::display(_('LDAP error'), $e->getMessage(), ERROR_DIALOG);
204 205 206 207 208
    }
  }

  function save()
  {
209 210 211 212 213 214
    return array();
  }

  function remove($fulldelete = FALSE)
  {
    return array();
215 216 217 218
  }

  function get_allowed_bases()
  {
219 220
    global $config;
    return $config->idepartments;
221 222 223
  }
}
?>