-
-
Save ergoz/cbb1e9d91d9846f0ad7c9d15a61d6a11 to your computer and use it in GitHub Desktop.
Ldap filter beautifier
This file contains 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
package main | |
import ( | |
"fmt" | |
"./ldap" | |
) | |
var filter = "(&(&(&(& (mailnickname=*) (| (&(objectCategory=person)(objectClass=user)(|(homeMDB=*)(msExchHomeServerName=*))) )))(objectCategory=user)(department=IT)(physicalDeliveryOfficeName=Amsterdam)))" | |
func main() { | |
fmt.Printf(ldap.FormatFilter(filter)) | |
} |
This file contains 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
# inspired by https://blogs.technet.microsoft.com/389thoughts/2016/11/24/ldap-query-prettyfier/ | |
function Format-LDAPFilter($ldapFilterString) { | |
$tabLevel = 0 | |
$indentationString = " " #you can do a single tab, two spaces, four spaces, etc. | |
$inline = $false | |
$ldapFilterStringRewritten = "" | |
#Eliminate whitespaces first. | |
$ldapFilterString = $ldapFilterString -replace "\s+","" | |
for ($i = 0; $i -lt $ldapFilterString.length; $i++) { | |
switch ($ldapFilterString[$i]) { | |
'('{ | |
$ldapFilterStringRewritten += "`n" | |
$ldapFilterStringRewritten += $indentationString * $tabLevel | |
$ldapFilterStringRewritten += "(" | |
$tabLevel += 1 | |
$inline = $false | |
} | |
')'{ | |
$tabLevel -= 1 | |
if (-NOT $inline) { | |
$ldapFilterStringRewritten += $indentationString * $tabLevel | |
} | |
$ldapFilterStringRewritten += ")" #`n | |
if (($ldapFilterString.length - $i) -gt 1 -AND $ldapFilterString[$i+1] -ne '(') { | |
$ldapFilterStringRewritten += "`n" | |
} | |
$inline = $false | |
} | |
default{ | |
$inline = $true | |
$ldapFilterStringRewritten += $ldapFilterString[$i] | |
} | |
} | |
} | |
$ldapFilterStringRewritten #implicit return | |
} |
This file contains 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
package ldap | |
import ( | |
"strings" | |
) | |
// inspired by https://blogs.technet.microsoft.com/389thoughts/2016/11/24/ldap-query-prettyfier/ | |
func FormatFilter(filter string) string { | |
buffer := new(strings.Builder) | |
noSpaces := strings.Replace(filter, " ", "", -1) | |
noSpaces = strings.Replace(noSpaces, "\n", "", -1) | |
tabLevel := 0 | |
tabString := " " | |
inline := false | |
for _, char := range noSpaces { | |
switch char { | |
case '(': | |
buffer.WriteString("\n") | |
buffer.WriteString(strings.Repeat(tabString, tabLevel)) | |
buffer.WriteString("(") | |
tabLevel = tabLevel + 1 | |
inline = false | |
case ')': | |
tabLevel = tabLevel - 1 | |
if !inline { | |
buffer.WriteString(strings.Repeat(tabString, tabLevel)) | |
} | |
buffer.WriteString(")\n") | |
inline = false | |
default: | |
inline = true | |
buffer.WriteRune(char) | |
} | |
} | |
return buffer.String() | |
} |
This file contains 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
function prettify($filter) | |
{ | |
$buffer = ""; | |
$noSpaces = str_replace(" ", "", $filter); | |
$noSpaces = str_replace("\n", "", $noSpaces); | |
$tabLevel = 0; | |
$tabString = " "; | |
$inline = false; | |
$chars = str_split($noSpaces); | |
foreach ($chars as $char) { | |
switch ($char) { | |
case '(': | |
$buffer .= "\n"; | |
$buffer .= str_repeat($tabString, $tabLevel); | |
$buffer .= "("; | |
$tabLevel++; | |
$inline = false; | |
break; | |
case ')': | |
$tabLevel--; | |
if (!$inline) { | |
$buffer .= str_repeat($tabString, $tabLevel); | |
} | |
$buffer .= ")\n"; | |
$inline = false; | |
break; | |
default: | |
$inline = true; | |
$buffer .= $char; | |
} | |
} | |
$buffer = preg_replace('/^[ \t]*[\r\n]+/m', '', $buffer); | |
return $buffer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment