« Ubuntu 效能調校備忘 | 首頁 | 防治垃圾信: 在 Sendmail、Postfix 加入 SPF 過濾功能 »

2008年11月24日

PHP 程式: ldapsearch 輸出內容 (LDIF) 轉 UTF-8 編碼

目的: 將 ldapsearch 輸出內容之 URL 編碼、Base64 編碼轉成 UTF-8 文字

附錄1: 讓 ldapadd 得以匯入 UTF-8 內容的 gecos 欄位

附錄2: 讓 phpldapadmin 匯出 UTF-8 編碼的 LDIF 內容

程式碼:


操作範例:

假設將以上程式命名為 utf8ldif.php

ex1.

ldapsearch -x -b "dc=example,dc=com" | tee myfile.ldif

php utf8ldif.php myfile.ldif | tee myfile_utf8.ldif

ex2.

ldapsearch -LLL -x -b "dc=example,dc=com" > myfile.ldif

cat myfile.ldif | php utf8ldif.php > myfile_utf8.ldif

ex3.

ldapsearch -x -b "dc=example,dc=com" | php utf8ldif.php


附錄1: 讓 ldapadd 得以匯入 UTF-8 內容的 gecos 欄位

vi /etc/openldap/schema/nis.schema

#原本:

attributetype ( 1.3.6.1.1.1.1.2 NAME 'gecos'
        desc 'The GECOS field; the common name'
        EQUALITY caseIgnoreIA5Match
        SUBSTR caseIgnoreIA5SubstringsMatch
        SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 SINGLE-VALUE )

#改成:

attributetype ( 1.3.6.1.1.1.1.2 NAME 'gecos'
        desc 'The GECOS field; the common name'
        EQUALITY caseIgnoreMatch
        SUBSTR caseIgnoreSubstringsMatch
        SYNTAX 1.3.6.1.4.1.1466.115.121.1.15{1024} )

/etc/init.d/ldap restart

Ref: http://phorum.study-area.org/index.php?topic=32935.0;wap2


附錄2: 讓 phpldapadmin 匯出 UTF-8 編碼的 LDIF 內容

vi /path/to/phpldapadmin/lib/export_functions.php

#var $MAX_LDIF_LINE_LENGTH = 76;
var $MAX_LDIF_LINE_LENGTH = 999;

# display dn
//if ($this->is_safe_ascii($dn))
    $output .= $this->multi_lines_display(sprintf('dn: %s',$dn));
//else
    //$output .= $this->multi_lines_display(sprintf('dn:: %s',base64_encode($dn)));

# display the attributes
foreach ($dndetails as $key => $attr) {
    if (! is_array($attr))
        $attr = array($attr);
        foreach ($attr as $value) {
            //if (! $this->is_safe_ascii($value) || $ldapserver->isAttrBinary($key)) {
                //$output .= $this->multi_lines_display(sprintf('%s:: %s',$key,base64_encode($value)));
            //} else {
                $output .= $this->multi_lines_display(sprintf('%s: %s',$key,$value));
            //}
    }
} # end foreach

Posted by Jamyy at 2008年11月24日 13:12