\Horde_Ldap_Ldif

LDIF capabilities for Horde_Ldap.

This class provides a means to convert between Horde_Ldap_Entry objects and LDAP entries represented in LDIF format files. Reading and writing are supported and manipulating of single entries or lists of entries.

Usage example: // Read and parse an LDIF file into Horde_Ldap_Entry objects // and print out the DNs. Store the entries for later use. $entries = array(); $ldif = new Horde_Ldap_Ldif('test.ldif', 'r', $options); do { $entry = $ldif->readEntry(); $dn = $entry->dn(); echo " done building entry: $dn\n"; $entries[] = $entry; } while (!$ldif->eof()); $ldif->done();

// Write those entries to another file $ldif = new Horde_Ldap_Ldif('test.out.ldif', 'w', $options); $ldif->writeEntry($entries); $ldif->done();

Copyright 2009 Benedikt Hallinger Copyright 2010-2017 Horde LLC (http://www.horde.org/)

Summary

Methods
Properties
Constants
__construct()
readEntry()
eof()
writeEntry()
writeVersion()
version()
handle()
done()
currentEntry()
parseLines()
currentLines()
nextLines()
No public properties found
No constants found
_changeEntry()
_writeEntry()
_convertAttribute()
_convertDN()
_isBinary()
_writeAttribute()
_writeDN()
_finishEntry()
_writeLine()
$_options
$_fh
$_fhOpened
$_inputLine
$_entrynum
$_mode
$_versionWritten
$_linesCur
$_linesNext
N/A
No private methods found
No private properties found
N/A

Properties

$_options

$_options : array

Options.

Type

array

$_fh

$_fh : resource

File handle for read/write.

Type

resource

$_fhOpened

$_fhOpened : boolean

Whether we opened the file handle ourselves.

Type

boolean

$_inputLine

$_inputLine : integer

Line counter for input file handle.

Type

integer

$_entrynum

$_entrynum : integer

Counter for processed entries.

Type

integer

$_mode

$_mode : string

Mode we are working in.

Either 'r', 'a' or 'w'

Type

string

$_versionWritten

$_versionWritten : boolean

Whether the LDIF version string was already written.

Type

boolean

$_linesCur

$_linesCur : array

Cache for lines that have built the current entry.

Type

array

$_linesNext

$_linesNext : array

Cache for lines that will build the next entry.

Type

array

Methods

__construct()

__construct(string|\ressource  $file, string  $mode = 'r', array  $options = array()) 

Constructor.

Opens an LDIF file for reading or writing.

$options is an associative array and may contain:

  • 'encode' (string): Some DN values in LDIF cannot be written verbatim and have to be encoded in some way. Possible values:
    • 'none': No encoding.
    • 'canonical': See {@link Horde_Ldap_Util::canonicalDN()}.
    • 'base64': Use base64 (default).
  • 'change' (boolean): Write entry changes to the LDIF file instead of the entries itself. I.e. write LDAP operations acting on the entries to the file instead of the entries contents. This writes the changes usually carried out by an update() to the LDIF file. Defaults to false.
  • 'lowercase' (boolean): Convert attribute names to lowercase when writing. Defaults to false.
  • 'sort' (boolean): Sort attribute names when writing entries according to the rule: objectclass first then all other attributes alphabetically sorted by attribute name. Defaults to false.
  • 'version' (integer): Set the LDIF version to write to the resulting LDIF file. According to RFC 2849 currently the only legal value for this option is 1. When this option is set Horde_Ldap_Ldif tries to adhere more strictly to the LDIF specification in RFC2489 in a few places. The default is null meaning no version information is written to the LDIF file.
  • 'wrap' (integer): Number of columns where output line wrapping shall occur. Default is 78. Setting it to 40 or lower inhibits wrapping.
  • 'raw' (string): Regular expression to denote the names of attributes that are to be considered binary in search results if writing entries. Example: 'raw' => '/(?i:^jpegPhoto|;binary)/i'

Parameters

string|\ressource $file

Filename or file handle.

string $mode

Mode to open the file, either 'r', 'w' or 'a'.

array $options

Options like described above.

Throws

\Horde_Ldap_Exception

readEntry()

readEntry() : \Horde_Ldap_Entry

Reads one entry from the file and return it as a Horde_Ldap_Entry object.

Throws

\Horde_Ldap_Exception

Returns

\Horde_Ldap_Entry

eof()

eof() : boolean

Returns true when the end of the file is reached.

Returns

boolean

writeEntry()

writeEntry(\Horde_Ldap_Entry|array  $entries) 

Writes the entry or entries to the LDIF file.

If you want to build an LDIF file containing several entries AND you want to call writeEntry() several times, you must open the file handle in append mode ('a'), otherwise you will always get the last entry only.

Parameters

\Horde_Ldap_Entry|array $entries

Entry or array of entries.

Throws

\Horde_Ldap_Exception

writeVersion()

writeVersion() 

Writes the version to LDIF.

If the object's version is defined, this method allows to explicitely write the version before an entry is written.

If not called explicitely, it gets called automatically when writing the first entry.

Throws

\Horde_Ldap_Exception

version()

version(integer  $version = null) : integer

Returns or sets the LDIF version.

If called with an argument it sets the LDIF version. According to RFC 2849 currently the only legal value for the version is 1.

Parameters

integer $version

LDIF version to set.

Throws

\Horde_Ldap_Exception

Returns

integer —

The current or new version.

handle()

handle() : resource

Returns the file handle the Horde_Ldap_Ldif object reads from or writes to.

You can, for example, use this to fetch the content of the LDIF file manually.

Throws

\Horde_Ldap_Exception

Returns

resource

done()

done() 

Cleans up.

This method signals that the LDIF object is no longer needed. You can use this to free up some memory and close the file handle. The file handle is only closed, if it was opened from Horde_Ldap_Ldif.

Throws

\Horde_Ldap_Exception

currentEntry()

currentEntry() : \Horde_Ldap_Entry

Returns the current Horde_Ldap_Entry object.

Throws

\Horde_Ldap_Exception

Returns

\Horde_Ldap_Entry

parseLines()

parseLines(array  $lines) : \Horde_Ldap_Entry

Parse LDIF lines of one entry into an Horde_Ldap_Entry object.

Parameters

array $lines

LDIF lines for one entry.

Throws

\Horde_Ldap_Exception

Returns

\Horde_Ldap_Entry

Horde_Ldap_Entry object for those lines.

currentLines()

currentLines() : array

Returns the lines that generated the current Horde_Ldap_Entry object.

Returns an empty array if no lines have been read so far.

Returns

array —

Array of lines.

nextLines()

nextLines(boolean  $force = false) : array

Returns the lines that will generate the next Horde_Ldap_Entry object.

If you set $force to true you can iterate over the lines that build up entries manually. Otherwise, iterating is done using {@link readEntry()}. $force will move the file pointer forward, thus returning the next entry lines.

Wrapped lines will be unwrapped. Comments are stripped.

Parameters

boolean $force

Set this to true if you want to iterate over the lines manually

Throws

\Horde_Ldap_Exception

Returns

array

_changeEntry()

_changeEntry(\Horde_Ldap_Entry  $entry) 

Writes an LDIF file that describes an entry change.

Parameters

\Horde_Ldap_Entry $entry

Throws

\Horde_Ldap_Exception

_writeEntry()

_writeEntry(\Horde_Ldap_Entry  $entry) 

Writes an LDIF file that describes an entry.

Parameters

\Horde_Ldap_Entry $entry

Throws

\Horde_Ldap_Exception

_convertAttribute()

_convertAttribute(string  $attr_name, string  $attr_value) : string

Converts an attribute and value to LDIF string representation.

It honors correct encoding of values according to RFC 2849. Line wrapping will occur at the configured maximum but only if the value is greater than 40 chars.

Parameters

string $attr_name

Name of the attribute.

string $attr_value

Value of the attribute.

Returns

string —

LDIF string for that attribute and value.

_convertDN()

_convertDN(string  $dn) : string

Converts an entry's DN to LDIF string representation.

It honors correct encoding of values according to RFC 2849.

Parameters

string $dn

UTF8 encoded DN.

Returns

string —

LDIF string for that DN.

_isBinary()

_isBinary(string  $value) : boolean

Returns whether some data is considered binary and must be base64-encoded.

Parameters

string $value

Some data.

Returns

boolean —

True if the data should be encoded.

_writeAttribute()

_writeAttribute(string  $attr_name, string|array  $attr_values) 

Writes an attribute to the file handle.

Parameters

string $attr_name

Name of the attribute.

string|array $attr_values

Single attribute value or array with attribute values.

Throws

\Horde_Ldap_Exception

_writeDN()

_writeDN(string  $dn) 

Writes a DN to the file handle.

Parameters

string $dn

DN to write.

Throws

\Horde_Ldap_Exception

_finishEntry()

_finishEntry() 

Finishes an LDIF entry.

Throws

\Horde_Ldap_Exception

_writeLine()

_writeLine(string  $line, string  $error = 'Unable to write to file handle') 

Writes an arbitary line to the file handle.

Parameters

string $line

Content to write.

string $error

If error occurs, throw this exception message.

Throws

\Horde_Ldap_Exception