#! /usr/bin/php changed.ldif * @author Cornelius Weiss * @copyright Copyright 2008 (c) CWTech (www.cwtech.de) * @license http://www.gnu.org/licenses/agpl.html * @version 0.2 */ /** * destination suffix */ $to = 'dc=cwtech,dc=de'; /** * line length of ldif file */ define("LDIF_LINELENGTH", 78); /* ---------- script startes here ---------- */ $line = fgets(STDIN); // old dc allways in first line: $from = trim(substr($line, 3)); while ($next = fgets(STDIN)) { // linebreak if(preg_match('/^ /', $next)) { $line = preg_replace('/\n/', '', $line) . substr($next, 1); continue; } // tranform dc of Top entry if (preg_match('/^dc: /', $line)) { preg_match('/dc=(.+),/', $to, $dc); $line = 'dc: ' . $dc[1] . "\n"; } // base64 encoded dn elseif(preg_match('/^dn:: /', $line)) { $b64 = substr($line ,4); $line = 'dn:: ' . base64_encode(preg_replace("/$from/", $to, base64_decode(trim($b64)))) . "\n"; } // NOTE: not only the dn needs to be transformed, also fields // like member, creator, ... needs must be handled $line = preg_replace("/$from/", $to, $line); // write to stdout fwrite(STDOUT, ldifwrap($line)); $line = $next; } /** * wraps input string into mulitiple lines * for ldif files * * @param string _instr * @return string wrapped string */ function ldifwrap($_instr) { $strlen = strlen($_instr); if ($strlen <= LDIF_LINELENGTH) return $_instr; $out = substr($_instr, 0 , LDIF_LINELENGTH) . "\n"; $i = LDIF_LINELENGTH; while (true) { if (($i + LDIF_LINELENGTH + 1) > $strlen) { $out .= ' ' . substr($_instr, $i); break; } $out .= ' ' . substr($_instr, $i, LDIF_LINELENGTH-1) . "\n"; $i += LDIF_LINELENGTH - 1; } return $out; }