Created
March 31, 2011 16:02
-
-
Save schakko/896652 to your computer and use it in GitHub Desktop.
Converts Active Directory LDIF schema to XML or JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This hack converts an Active Directory LDIF schema file to XML or JSON. | |
* Export Active Directory schema with the following command (must be run as Domain Administrator): | |
* ldifde -f SaveSchema.ldif -d CN=Schema,CN=Configuration,DC=domain,DC=local | |
* Change var $file to location of generated schema, open your browser, point to this file and append ?xml for XML output or ?json for JSON output | |
* | |
* @author Christopher Klein <ckl[at]ecw[dot]de> | |
* @url http://wap.ecw.de | |
*/ | |
$file = "SaveSchema.ldif"; | |
/** | |
* Creates an array with attribute meta data. Every item is one plain attribute meta data element | |
* @param string path to .ldif file | |
* @return array | |
*/ | |
function extractAttributesAsText($fileName) { | |
$content = file_get_contents($fileName); | |
$arrLines = explode("\n", $content); | |
$r = array(); | |
$plainAttribute = ""; | |
for ($i = 0, $m = sizeof($arrLines); $i < $m; $i++) { | |
$line = $arrLines[$i]; | |
if ((strlen(trim($line)) == 0) || (($i + 1) == $m)) { | |
array_push($r, $plainAttribute); | |
$plainAttribute = ""; | |
} | |
else { | |
$plainAttribute .= $line . "\n"; | |
} | |
} | |
return $r; | |
} | |
/** | |
* push attribute value to attribute list. | |
* if attribute name already exist, the value will be converted to array and existent element is pushed onto a new created stack | |
* @param array reference to attribute list | |
* @param string name of attribute | |
* @param string value of attribute | |
*/ | |
function pushAttributeValueToStack(&$arrAttributes, $attributeName, $attributeValue) { | |
if (isset($arrAttributes[$attributeName])) { | |
if (!is_array($arrAttributes[$attributeName])) { | |
$existentValue = $arrAttributes[$attributeName]; | |
$arrAttributes[$attributeName] = array($existentValue); | |
} | |
array_push($arrAttributes[$attributeName], $attributeValue); | |
} | |
else { | |
$arrAttributes[$attributeName] = $attributeValue; | |
} | |
} | |
/** | |
* converts every attribute to an array item | |
* @param string plain attribute meta data | |
* @return array | |
*/ | |
function convertPlainAttributesToArray($plainAttribute) { | |
$r = array(); | |
$arrLines = explode("\n", $plainAttribute); | |
$lastAttributeName = null; | |
$lastAttributeValue = null; | |
foreach ($arrLines as $line) { | |
// attribute line found | |
if (preg_match("/^(\w+):\s(.*)$/", $line, $arrMatches)) { | |
if ($lastAttributeName != null) { | |
pushAttributeValueToStack($r, $lastAttributeName, $lastAttributeValue); | |
} | |
$lastAttributeName = $arrMatches[1]; | |
$lastAttributeValue = trim($arrMatches[2]); | |
} | |
// multi-line attribute | |
elseif ($line{0} == ' ') { | |
$lastAttributeValue .= trim($line); | |
} | |
} | |
// push last element to stack | |
if (lastAttributeName != null) { | |
pushAttributeValueToStack($r, $lastAttributeName, $lastAttributeValue); | |
} | |
return $r; | |
} | |
/** | |
* small function for converting our array to a nested XML structure | |
* @param XMLWriter XML writer object | |
* @param array array with converted LDIF array structure | |
*/ | |
function convertArrayToXml($xmlWriter, $arrData) { | |
foreach($arrData as $key => $value){ | |
if (is_int($key)) { | |
$key = 'entry'; | |
} | |
if (is_array($value)){ | |
$xmlWriter->startElement($key); | |
convertArrayToXml($xmlWriter, $value); | |
$xmlWriter->endElement(); | |
continue; | |
} | |
if ($key) { | |
$xmlWriter->writeElement($key, $value); | |
} | |
} | |
} | |
// extract every meta attribute to an array | |
$arrAttributesAsText = extractAttributesAsText($file); | |
$arrData = array(); | |
// convert every attribute from meta attribute to an array | |
foreach ($arrAttributesAsText as $plainAttribute) { | |
$arrConverted = convertPlainAttributesToArray($plainAttribute); | |
if (is_array($arrConverted) && (sizeof($arrConverted) > 0)) { | |
array_push($arrData, $arrConverted); | |
} | |
} | |
// send file as XML | |
if (isset($_GET['xml'])) { | |
header("Content-type: application/xml"); | |
header("Content-Disposition:attachment;filename=" . $file . ".xml"); | |
$xmlWriter = new XmlWriter(); | |
$xmlWriter->openMemory(); | |
$xmlWriter->startDocument('1.0', 'UTF-8'); | |
$xmlWriter->startElement('root'); | |
convertArrayToXml($xmlWriter, $arrData); | |
$xmlWriter->endElement(); | |
echo $xmlWriter->outputMemory(true); | |
exit; | |
} | |
// send file as JSON | |
if (isset($_GET['json'])) { | |
header("Content-Type: application/json"); | |
header("Content-Disposition:attachment;filename=" . $file . ".json"); | |
print json_encode($arrData); | |
exit; | |
} | |
?> | |
<a href="<?= $PHP_SELF ?>?xml">XML</a> | <a href="<?= $PHP_SELF ?>?json">JSON</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment