ActiveDirectory
[ class tree: ActiveDirectory ] [ index: ActiveDirectory ] [ all elements ]

Source for file API.php

Documentation is available at API.php

  1. <?php
  2. /**
  3.  * Project: ActiveDirectory :: Control Active Directory with ldap or ldaps
  4.  * File:    ActiveDirectory.php
  5.  *
  6.  * Copyright (c) 2018, JoungKyun.Kim <http://oops.org>
  7.  *
  8.  * LICENSE: BSD
  9.  *
  10.  * ActiveDirectory pear package support to control Microsoft Active Directory
  11.  * with ldap or ldaps protocol.
  12.  *
  13.  * @category   System
  14.  * @package    ActiveDirectory
  15.  * @author     JoungKyun.Kim <http://oops.org>
  16.  * @copyright  (c) 2018 JoungKyun.Kim
  17.  * @license    BSD License
  18.  * @link       http://pear.oops.org/package/ActiveDirectory
  19.  * @filesource
  20.  */
  21.  
  22. /**
  23.  * import LDAP_API class
  24.  */
  25. require_once 'ActiveDirectory/Common.php';
  26.  
  27. /**
  28.  * ActiveDirectory_API :: Active Directory Internal API
  29.  * @package ActiveDirectory
  30.  */
  31. {
  32.     // {{{ properties
  33.     /**
  34.      * Set pagenation value
  35.      * @access  private
  36.      * @var     integer 
  37.      */
  38.     private $pagesize 1000;
  39.  
  40.     /**
  41.      * Unix attribute entry
  42.      * @access  private
  43.      * @var     array 
  44.      */
  45.     private $unix_attr array (
  46.         'uid''uidnumber''gidnumber''loginshell''mssfu30name',
  47.         'mssfu30nisdomain''unixhomedirectory''unixuserpassword'
  48.     );
  49.     // }}}
  50.  
  51.     // {{{ (boolean) ActiveDirectory_API::auth ($rdn, $pass)
  52.     /**
  53.      * bind 암호를 인증
  54.      *
  55.      * @access  protected
  56.      * @return  boolean 
  57.      * @param   string   bind DN
  58.      * @param   string   bind 암호
  59.      */
  60.     
  61. protected
  62.     function auth ($rdn$pass$link null{
  63.         $link $link $link $this->link;
  64.         if is_resource ($link) )
  65.             return ActiveDirectory::AD_FAILURE;
  66.  
  67.         return @ldap_bind ($link$rdn$pass);
  68.     }
  69.     // }}}
  70.  
  71.     // {{{ (mixed) ActiveDirectory_API::search_api ($rdn, $filter = null, $attr = null)
  72.     /**
  73.      * Entry를 검색
  74.      *
  75.      * 기본적으로 Active Directory는 검색 결과를 1000개로 제한을 한다. 1000개
  76.      * 이상의 검색 결과를 가져야 한다면 PHP 5.4 이상을 사용하거나 또는 5.3 이하
  77.      * 버전에서는 ldap extnesion에 pagenation page를 해 줘야한다.
  78.      *
  79.      * 이 API를 PHP 5.3 이하 버전에서 pagenation 패치가 되지 않은 상태에서
  80.      * 실행하면 최대 1000개의 결과를 반환한다.
  81.      *
  82.      * @access  protected
  83.      * @return  object|array|false
  84.      * @param   string   Bind DN
  85.      * @param   string   검색 필터
  86.      * @param   array    반환할 속성
  87.      */
  88.     
  89. protected
  90.     function search_api ($rdn$filter null$attr null$link null{
  91.         $filter $filter $filter '(objectclass=*)';
  92.         $attr   is_array ($attr$attr array ();
  93.         $link   $link $link $this->link;
  94.         $rdn    $rdn  $rdn  $this->rdn;
  95.  
  96.         # filter에 euc-kr(또는 cp949)가 있으면 utf8로 변환한다
  97.         # Need KSC5601 pear package
  98.         # If you use local character set except euc-kr,
  99.         # this code changes to iconv
  100.         if $this->ksc->is_utf8 ($filter=== false )
  101.             $filter $this->ksc->utf8 ($filter);
  102.  
  103.         if function_exists ('ldap_control_paged_results') ) {
  104.             #
  105.             # Support from PHP 5.4 or ldap pagenation patch
  106.             #
  107.             $cookie '';
  108.             $ent array ();
  109.  
  110.             $i 0;
  111.             do {
  112.                 ldap_control_paged_results ($link$this->pagesizetrue$cookie);
  113.  
  114.                 if ( ($entries @ldap_search ($link$rdn$filter$attr0030)) === false {
  115.                     ActiveDirectory::$error ldap_error ($link);
  116.                     return false;
  117.                 }
  118.  
  119.                 #ldap_sort ($link, $entries, 'sn');
  120.                 $ent ldap_get_entries ($link$entries);
  121.  
  122.                 if is_array ($ent&& $ent['count'!= {
  123.                     $this->search_merge ($r$ent);
  124.                 }
  125.  
  126.                 ldap_control_paged_results_response ($link$entries$cookie);
  127.                 $i++;
  128.             while $cookie !== null && $cookie != '' );
  129.         else {
  130.  
  131.             if ( ($entries @ldap_search ($link$rdn$filter$attr0030)) === false {
  132.                 ActiveDirectory::$error ldap_error ($link);
  133.                 return false;
  134.             }
  135.  
  136.             ldap_sort ($link$entries'sn');
  137.  
  138.             $ent ldap_get_entries ($link$entries);
  139.  
  140.             if is_array ($ent|| $ent['count'== {
  141.                 ActiveDirectory::$error sprintf ("%s condition don't exists"$filter);
  142.                 return false;
  143.             }
  144.  
  145.             $this->search_merge ($r$ent);
  146.         }
  147.  
  148.         if count ($r== )
  149.             return $r[0];
  150.  
  151.         return $r;
  152.     }
  153.     // }}}
  154.  
  155.     // {{{ (void) ActiveDirectory_API::search_merge (&$r, $ent)
  156.     /**
  157.      * 검색 결과를 merge.
  158.      *
  159.      * @access  private
  160.      * @return  void 
  161.      * @param   array  결과값을 저장할 배열
  162.      *           array  merge할 배열 데이터
  163.      */
  164.     
  165. private
  166.     function search_merge (&$r$ent{
  167.         if is_array ($r) )
  168.             $r array ();
  169.  
  170.         if $ent['count'== )
  171.             return;
  172.  
  173.         $ignore_pattern '/^(object|msexchmailboxguid|msexchmailboxsecuritydescriptor|count|usercerti|logonhours|userparameters|replicationsignature|msmqsigncertifi|msmqdigests)/i';
  174.         $keyno count ($r);
  175.  
  176.         $this->fix_charset ($ent);
  177.  
  178.         foreach $ent as $key => $value {
  179.             if $key === 'count' )
  180.                 continue;
  181.  
  182.             if is_array ($value) )
  183.                 $value array ();
  184.  
  185.             $key += $keyno;
  186.  
  187.             foreach ($value as $k => $v{
  188.                 if is_numeric ($k|| preg_match ($ignore_pattern$k) )
  189.                     continue;
  190.  
  191.                 if $v['count'== {
  192.                     if preg_match ('/0Z$/'$v[0]) )
  193.                         $this->convert_to_unixtime_from_0Ztime ($v[0]);
  194.  
  195.                     $r[$key]->$k $v[0];
  196.                 else
  197.                     $r[$key]->$k $v;
  198.             }
  199.  
  200.             $this->convert_to_unixtime ($r[$key]->badpasswordtime);
  201.             $this->convert_to_unixtime ($r[$key]->lastlogon);
  202.             $this->convert_to_unixtime ($r[$key]->pwdlastset);
  203.             $this->convert_to_unixtime ($r[$key]->accountexpires);
  204.             $this->convert_to_unixtime ($r[$key]->lastlogontimestamp);
  205.  
  206.             if $r[$key]->member['count')
  207.                 $member $r[$key]->member;
  208.             else
  209.                 $member $r[$key]->memberof;
  210.  
  211.             for $i 0$i<$member['count']$i++ )
  212.                 $r[$key]->members[$ipreg_replace ('/(CN=|,.*)/i'''$member[$i]);
  213.  
  214.         }
  215.     }
  216.     // }}}
  217.  
  218.     // {{{ (string) ActiveDirectory_API::make_nt_password ($pass)
  219.     /**
  220.      * UTF-16 기반의 Active Directory 암호를 생성
  221.      *
  222.      * @access protected
  223.      * @return string 
  224.      * @param  string 암호 문자열
  225.      */
  226.     
  227. protected
  228.     function make_nt_password ($pass{
  229.         $pass sprintf ('"%s"'$pass);
  230.         $passlen strlen ($pass);
  231.  
  232.         for $i=0$i<$passlen$i++ )
  233.             $newpass .= $pass[$i"\000";
  234.  
  235.         return $newpass;
  236.     }
  237.     // }}}
  238.  
  239.     // {{{ (boolean) ActiveDirectory_API::set_unix_attributes ($account, $type = 'add') {
  240.     /**
  241.      * Unix attributte를 활성/수정/비활성 한다.
  242.      *
  243.      * 제어하는 Unix attribute는 다음과 같다.
  244.      *
  245.      * <ol>
  246.      *     <li>uid</li>
  247.      *     <li>uidnumber</li>
  248.      *     <li>gidnumber</li>
  249.      *     <li>mssfu30name</li>
  250.      *     <li>mssfu30nisdomain</li>
  251.      *     <li>loginshell</li>
  252.      *     <li>unixhomedirectory</li>
  253.      *     <li>unixuserpassword</li>
  254.      * </ol>
  255.      *
  256.      * 삭제시, uid, unixuserpassword, mssfu30name 은 남아 있는다.
  257.      *
  258.      * @access protected
  259.      * @return boolean 
  260.      * @param  object|string계정 속성 또는 계정 이름
  261.      * @param array  Unix Attribute 값
  262.      * @param string 활성/비활성/수정 (add/replace/remove)
  263.      */
  264.     
  265. protected
  266.     function set_unix_attributes ($id$attr$type 'add'{
  267.         if is_object ($account) )
  268.             $account $this->user ($account);
  269.  
  270.         if $account === false || is_object ($account) )
  271.             return false;
  272.  
  273.         $this->set_array ($attr);
  274.  
  275.         $is_unix $this->is_unix_attribute ($account);
  276.         $r true;
  277.  
  278.         switch ($type{
  279.             case 'add' :
  280.                 if $is_unix )
  281.                     return true;
  282.  
  283.                 $buf $this->maxid ();
  284.                 $uidnumber $buf->uid;
  285.                 $gidnumber $buf->gid;
  286.  
  287.                 if $attr->pass )
  288.                     $attr->unixuserpassword 'ABCD!efgh12345$67890';
  289.                 else {
  290.                     if preg_match ('/^\$1\$/'$attr->unixuserpassword) )
  291.                         $attr->unixuserpassword crypt ($attr->unixuserpassword);
  292.                 }
  293.  
  294.                 if $attr->loginshell )
  295.                     $attr->loginshell '/bin/bash';
  296.  
  297.                 if $attr->mssfu30name )
  298.                     $attr->mssfu30name $account->cn;
  299.  
  300.                 if $attr->mssfu30nisdomain )
  301.                     $attr->mssfu30nisdomain $this->domain;
  302.  
  303.                 if $attr->unixhomedirectory )
  304.                     $attr->unixhomedirectory '/home/AD/' $account->cn;
  305.  
  306.                 $value array (
  307.                     'uid'               => $account->cn,
  308.                     'uidnumber'         => ++$uidnumber,
  309.                     'gidnumber'         => $gidnumber,
  310.                     'loginshell'        => $attr->shell,
  311.                     'mssfu30name'       => $attr->mssfu30name,
  312.                     'mssfu30nisdomain'  => $attr->mssfu30nisdomain,
  313.                     'unixhomedirectory' => $attr->unixhomedirectory,
  314.                     'unixuserpassword'  => $attr->unixuserpassword,
  315.                 );
  316.  
  317.                 $addvar array ();
  318.                 foreach $value as $k => $v {
  319.                     if isset ($account->$k) )
  320.                         $addvar[$k$value[$k];
  321.                 }
  322.  
  323.                 $r $this->exec ($account->distinguishedname$addvar'add');
  324.                 break;
  325.             case 'replace' :
  326.                 if $is_unix )
  327.                     return $this->set_unix_attirbutes ($account$attr);
  328.  
  329.                 if $attr->unixuserpassword {
  330.                     if preg_match ('/^\$1\$/'$attr->unixuserpassword) )
  331.                         $attr->unixuserpassword crypt ($attr->unixuserpassword);
  332.                 }
  333.  
  334.                 $attrkey array ('unixuserpassword''loginshell''unixhomedirectory');
  335.                 foreach $attrkey as $key {
  336.                     $act $account->$key 'replace' 'add';
  337.                     $this->exec (
  338.                         $account->distinguishedname,
  339.                         array ($key => $attr->$key),
  340.                         $act
  341.                     );
  342.                 }
  343.  
  344.                 break;
  345.             default :
  346.                 if $is_unix )
  347.                     return true;
  348.  
  349.                 $entries array (
  350.                     'uid''uidnumber''gidnumber''loginshell''mssfu30name',
  351.                     'mssfu30nisdomain''unixhomedirectory''unixuserpassword'
  352.                 );
  353.  
  354.                 foreach $entryies as $key {
  355.                     if $account->$key )
  356.                         $value[$key$account->$ey;
  357.                 }
  358.  
  359.                 if is_array ($value&& count ($value)
  360.                     $r $this->exec ($account->distinguishedname$value'del');
  361.         }
  362.  
  363.         if $r === false {
  364.             ActiveDirectory::$error ActiveDirectory::$error .
  365.                 ' (' $account->cn ' - ' $account->distinguishedname ')';
  366.         }
  367.  
  368.         return $r;
  369.     }
  370.     // }}}
  371. }
  372.  
  373. /*
  374.  * Local variables:
  375.  * tab-width: 4
  376.  * c-basic-offset: 4
  377.  * End:
  378.  * vim: set filetype=php noet sw=4 ts=4 fdm=marker:
  379.  * vim600: noet sw=4 ts=4 fdm=marker
  380.  * vim<600: noet sw=4 ts=4
  381.  */
  382. ?>

Documentation generated on Tue, 14 May 2019 01:59:40 +0900 by phpDocumentor 1.4.4