Documentation

Horde_Ldap_Ldif
in package

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/)

Tags
category

Horde

author

Benedikt Hallinger beni@php.net

author

Jan Schneider jan@horde.org

license

http://www.gnu.org/licenses/lgpl-3.0.html LGPL-3.0

see
http://www.ietf.org/rfc/rfc2849.txt
todo

LDAPv3 controls are not implemented yet

Table of Contents

$_entrynum  : int
Counter for processed entries.
$_fh  : resource
File handle for read/write.
$_fhOpened  : bool
Whether we opened the file handle ourselves.
$_inputLine  : int
Line counter for input file handle.
$_linesCur  : array<string|int, mixed>
Cache for lines that have built the current entry.
$_linesNext  : array<string|int, mixed>
Cache for lines that will build the next entry.
$_mode  : string
Mode we are working in.
$_options  : array<string|int, mixed>
Options.
$_versionWritten  : bool
Whether the LDIF version string was already written.
__construct()  : mixed
Constructor.
currentEntry()  : Horde_Ldap_Entry
Returns the current Horde_Ldap_Entry object.
currentLines()  : array<string|int, mixed>
Returns the lines that generated the current Horde_Ldap_Entry object.
done()  : mixed
Cleans up.
eof()  : bool
Returns true when the end of the file is reached.
handle()  : resource
Returns the file handle the Horde_Ldap_Ldif object reads from or writes to.
nextLines()  : array<string|int, mixed>
Returns the lines that will generate the next Horde_Ldap_Entry object.
parseLines()  : Horde_Ldap_Entry
Parse LDIF lines of one entry into an Horde_Ldap_Entry object.
readEntry()  : Horde_Ldap_Entry
Reads one entry from the file and return it as a Horde_Ldap_Entry object.
version()  : int
Returns or sets the LDIF version.
writeEntry()  : mixed
Writes the entry or entries to the LDIF file.
writeVersion()  : mixed
Writes the version to LDIF.
_changeEntry()  : mixed
Writes an LDIF file that describes an entry change.
_convertAttribute()  : string
Converts an attribute and value to LDIF string representation.
_convertDN()  : string
Converts an entry's DN to LDIF string representation.
_finishEntry()  : mixed
Finishes an LDIF entry.
_isBinary()  : bool
Returns whether some data is considered binary and must be base64-encoded.
_writeAttribute()  : mixed
Writes an attribute to the file handle.
_writeDN()  : mixed
Writes a DN to the file handle.
_writeEntry()  : mixed
Writes an LDIF file that describes an entry.
_writeLine()  : mixed
Writes an arbitary line to the file handle.

Properties

$_entrynum

Counter for processed entries.

protected int $_entrynum = 0

$_fh

File handle for read/write.

protected resource $_fh

$_fhOpened

Whether we opened the file handle ourselves.

protected bool $_fhOpened = \false

$_inputLine

Line counter for input file handle.

protected int $_inputLine = 0

$_linesCur

Cache for lines that have built the current entry.

protected array<string|int, mixed> $_linesCur = array()

$_linesNext

Cache for lines that will build the next entry.

protected array<string|int, mixed> $_linesNext = array()

$_mode

Mode we are working in.

protected string $_mode

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

$_options

Options.

protected array<string|int, mixed> $_options = array('encode' => 'base64', 'change' => \false, 'lowercase' => \false, 'sort' => \false, 'version' => \null, 'wrap' => 78, 'raw' => '')

$_versionWritten

Whether the LDIF version string was already written.

protected bool $_versionWritten = \false

Methods

__construct()

Constructor.

public __construct(string|ressource $file[, string $mode = 'r' ][, array<string|int, mixed> $options = array() ]) : mixed

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 . - '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
$file : string|ressource

Filename or file handle.

$mode : string = 'r'

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

$options : array<string|int, mixed> = array()

Options like described above.

Tags
throws
Horde_Ldap_Exception
Return values
mixed

currentLines()

Returns the lines that generated the current Horde_Ldap_Entry object.

public currentLines() : array<string|int, mixed>

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

Return values
array<string|int, mixed>

Array of lines.

done()

Cleans up.

public done() : mixed

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.

Tags
throws
Horde_Ldap_Exception
Return values
mixed

eof()

Returns true when the end of the file is reached.

public eof() : bool
Return values
bool

handle()

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

public handle() : resource

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

Tags
throws
Horde_Ldap_Exception
Return values
resource

nextLines()

Returns the lines that will generate the next Horde_Ldap_Entry object.

public nextLines([bool $force = false ]) : array<string|int, mixed>

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

Wrapped lines will be unwrapped. Comments are stripped.

Parameters
$force : bool = false

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

Tags
throws
Horde_Ldap_Exception
Return values
array<string|int, mixed>

parseLines()

Parse LDIF lines of one entry into an Horde_Ldap_Entry object.

public parseLines(array<string|int, mixed> $lines) : Horde_Ldap_Entry
Parameters
$lines : array<string|int, mixed>

LDIF lines for one entry.

Tags
todo

what about file inclusions and urls? "jpegphoto:< file:///usr/local/directory/photos/fiona.jpg"

throws
Horde_Ldap_Exception
Return values
Horde_Ldap_Entry

Horde_Ldap_Entry object for those lines.

version()

Returns or sets the LDIF version.

public version([int $version = null ]) : int

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
$version : int = null

LDIF version to set.

Tags
throws
Horde_Ldap_Exception
Return values
int

The current or new version.

writeEntry()

Writes the entry or entries to the LDIF file.

public writeEntry(Horde_Ldap_Entry|array<string|int, mixed> $entries) : mixed

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
$entries : Horde_Ldap_Entry|array<string|int, mixed>

Entry or array of entries.

Tags
todo

Implement operations on whole entries (adding a whole entry).

throws
Horde_Ldap_Exception
Return values
mixed

writeVersion()

Writes the version to LDIF.

public writeVersion() : mixed

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.

Tags
throws
Horde_Ldap_Exception
Return values
mixed

_convertAttribute()

Converts an attribute and value to LDIF string representation.

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

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
$attr_name : string

Name of the attribute.

$attr_value : string

Value of the attribute.

Return values
string

LDIF string for that attribute and value.

_convertDN()

Converts an entry's DN to LDIF string representation.

protected _convertDN(string $dn) : string

It honors correct encoding of values according to RFC 2849.

Parameters
$dn : string

UTF8 encoded DN.

Tags
todo

I am not sure, if the UTF8 stuff is correctly handled right now

Return values
string

LDIF string for that DN.

_finishEntry()

Finishes an LDIF entry.

protected _finishEntry() : mixed
Tags
throws
Horde_Ldap_Exception
Return values
mixed

_isBinary()

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

protected _isBinary(string $value) : bool
Parameters
$value : string

Some data.

Return values
bool

True if the data should be encoded.

_writeAttribute()

Writes an attribute to the file handle.

protected _writeAttribute(string $attr_name, string|array<string|int, mixed> $attr_values) : mixed
Parameters
$attr_name : string

Name of the attribute.

$attr_values : string|array<string|int, mixed>

Single attribute value or array with attribute values.

Tags
throws
Horde_Ldap_Exception
Return values
mixed

_writeDN()

Writes a DN to the file handle.

protected _writeDN(string $dn) : mixed
Parameters
$dn : string

DN to write.

Tags
throws
Horde_Ldap_Exception
Return values
mixed

_writeLine()

Writes an arbitary line to the file handle.

protected _writeLine(string $line[, string $error = 'Unable to write to file handle' ]) : mixed
Parameters
$line : string

Content to write.

$error : string = 'Unable to write to file handle'

If error occurs, throw this exception message.

Tags
throws
Horde_Ldap_Exception
Return values
mixed

Search results