예제 #1
0
 /**
  * @staticvar CLdapConnector|null $oLdap
  * @param CAccount $oAccount
  * @return CLdapConnector|bool
  */
 private function Ldap($oAccount)
 {
     //		if ($oAccount)
     //		{
     //			// TODO
     //			$aCustomFields = $oAccount->CustomFields;
     //			$aCustomFields['LdapPabUrl'] = 'ldap://192.168.0.197:389/ou=TestUser2,ou=PAB,dc=example,dc=com';
     //			$aCustomFields['LdapPabUrl'] = 'ldap://jes7dir.netvision.net.il:389/ou=24606995,ou=People,o=netvision.net.il,o=NVxSP,o=pab';
     //			$oAccount->CustomFields = $aCustomFields;
     //		}
     static $aLdap = array();
     if (!$oAccount || !isset($oAccount->CustomFields) || empty($oAccount->CustomFields['LdapPabUrl'])) {
         return false;
     }
     $sPabUrl = $oAccount->CustomFields['LdapPabUrl'];
     $aPabUrl = api_Utils::LdapUriParse($sPabUrl);
     if (isset($aLdap[$sPabUrl]) && $aLdap[$sPabUrl]) {
         return $aLdap[$sPabUrl];
     }
     if (!extension_loaded('ldap')) {
         CApi::Log('LDAP: Can\'t load LDAP extension.', ELogLevel::Error);
         return false;
     }
     if (!class_exists('CLdapConnector')) {
         CApi::Inc('common.ldap');
     }
     $oLdap = new CLdapConnector($aPabUrl['search_dn']);
     $oLdap = $oLdap->Connect((string) $aPabUrl['host'], (int) $aPabUrl['port'], (string) CApi::GetConf('contacts.ldap.bind-dn', ''), (string) CApi::GetConf('contacts.ldap.bind-password', '')) ? $oLdap : false;
     if ($oLdap) {
         if (!$oLdap->Search('(objectClass=*)')) {
             CApi::Log('LDAP: Init PabUrl Entry');
             $sNewDn = $oLdap->GetSearchDN();
             $aDnExplode = ldap_explode_dn($sNewDn, 1);
             $sOu = isset($aDnExplode[0]) ? trim($aDnExplode[0]) : '';
             $aPabUrlEntry = CApi::GetConf('contacts.ldap.pab-url-entry', array('objectClass' => array('top', 'organizationalUnit')));
             if (isset($aPabUrlEntry['objectClass'])) {
                 $aPabUrlEntry['ou'] = $sOu;
                 if (0 < strlen($sOu)) {
                     if (!$oLdap->Add('', $aPabUrlEntry)) {
                         $oLdap = false;
                     }
                 } else {
                     CApi::Log('LDAP: empty Ou in SearchDn = ' . $sNewDn);
                     $oLdap = false;
                 }
             } else {
                 CApi::Log('LDAP: pab-url-entry format error');
                 CApi::Log(print_r($aPabUrlEntry, true));
                 $oLdap = false;
             }
         }
     }
     $aLdap[$sPabUrl] = $oLdap;
     return $oLdap;
 }