php ldap アクセスサンプルプログラム

<?php // -*- mode:PHP; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-

class LdapAccess
{
    
    var $hostname               = NULL;
    var $port                   = NULL;
    var $ldap_set_option_option = NULL;
    var $ldap_set_option_newval = NULL;
    var $link_id                = NULL;
    var $search_result          = NULL;

    // hostnameを設定する。
    function setHostname($hostname)
    {
        $this->hostname = $hostname;
    }
    // portを設定する。
    function setPort($port)
    {
        $this->port = $port;
    }


    function connect_start(){
        if(!($this->wrapper_ldap_connect()))
            {
                return false;
            }
        else if(!($this->wrapper_ldap_set_option()))
            {
                return false;
            }
        else if(!($this->wrapper_ldap_bind()))
            {
                return false;
            }
        else
            {
                return true;
            }
    }
    
    // LDAP 接続
    function wrapper_ldap_connect()
    {
        $this->link_id = ldap_connect($this->hostname,$this->port);  
        if(!$this->link_id)
            {
                return false;
            }
        else
            {
                return true;
            }
    }
    
    function wrapper_ldap_errno()
    {
        return ldap_errno($this->link_id);
    }

    function wrapper_ldap_error()
    {
        return ldap_error($this->link_id);
    }


    function wrapper_ldap_set_option()
    {
        return ldap_set_option($this->link_id,$this->ldap_set_option_option,$this->ldap_set_option_newval);
    }

    //ldap_set_optionのoptionを設定する。
    function setLdap_set_option_option($option)
    {
        $this->ldap_set_option_option = $option;
    }

    //ldap_set_optionのnewvalを設定する。
    function setLdap_set_option_newval($newval)
    {
        $this->ldap_set_option_newval = $newval;
    }

    // bind_rdnを設定する。
    function setBind_rdn($bind_rdn)
    {
        $this->bind_rdn = $bind_rdn;
    }
    // bind_passwordを設定する。
    function setBind_password($bind_password)
    {
        $this->bind_password = $bind_password;
    }


    //ldap_bind -- LDAP ディレクトリにバインドする
    function wrapper_ldap_bind()
    {
        return ldap_bind($this->link_id,$this->bind_rdn,$this->bind_password);
    }

    
    function wrapper_ldap_search($base_dn,$filter,$justthese=null)
    {
        if(is_array($justthese))
            {
                $this->search_result=ldap_search($this->link_id,$base_dn,$filter,$justthese);
            }
        else
            {
                $this->search_result=ldap_search($this->link_id,$base_dn,$filter);
            }
    }
    function wrapper_ldap_mod_replace($dn,$entry)
    {      
        return ldap_mod_replace($this->link_id,$dn,$entry);
    }
    function wrapper_ldap_add($dn,$entry)
    {      
        return ldap_add($this->link_id,$dn,$entry);
    }
    
    
    function wrapper_ldap_count_entries()
    {
        return ldap_count_entries($this->link_id,$this->search_result);
    }

    function wrapper_ldap_get_entries()
    {
        return ldap_get_entries($this->link_id,$this->search_result);
    }

    // LDAP 切断
    function wrapper_ldap_close()
    {
        ldap_close($this->link_id);
    }
}


$ldap = new LdapAccess;
$ldap->setHostname("ldap://hoge.hoge.com:389");
$ldap->setLdap_set_option_option(LDAP_OPT_PROTOCOL_VERSION);
$ldap->setLdap_set_option_newval(3);
$ldap->setBind_rdn("cn=hoge,o=ldap,dc=hoge,dc=com");
$ldap->setBind_password("_hoge");

if($ldap->connect_start())
    {
        $ldap->wrapper_ldap_search("o=hoge,dc=hoge,dc=com","(&(objectClass=organizationalUnit)(ou;lang-ja=*))");
        print_r($ldap->wrapper_ldap_get_entries());
        print $ldap->wrapper_ldap_count_entries();
    }
else
    {
        print $ldap->wrapper_ldap_error();
        print $ldap->wrapper_ldap_errno();
    }
$ldap->wrapper_ldap_close();

?>


php curl sampleプログラム

https対応、basic認証対応

<?php
class curl 
{
    var $id = null;
    var $pw = null;
    var $url = null;
    var $fieldsdata = null;
    var $_result = array();
    var $response_code = null;
    function set_id($id) {
        $this->id = $id;
    }

    function set_pw($pw) {
        $this->pw = $pw;
    }
  
    function set_url($url) {
        $this->url = $url;
    }

    var $proxy;
    function set_proxy($proxy) {
        $this->proxy = $proxy;
    }


    var $proxyport;
    function set_proxyport($proxyport) {
        $this->proxyport = $proxyport;
    }

    function get_url() {
        return $this->url;
    }

    function set_fieldsdata($value) {
        $this->fieldsdata = $value;
    }

    function get_fieldsdata() {
        return $this->fieldsdata;
    }

    function get_result() {
        return $this->_result;
    }

    function do_connect() {
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $this->url);
        curl_setopt ($ch, CURLOPT_HEADER, 1);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,  false);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);

        if($this->proxy)
          {
            curl_setopt($ch, CURLOPT_PROXY,$this->proxy);
          }
        if($this->proxyport)
          {
            curl_setopt($ch, CURLOPT_PROXYPORT,$this->proxyport);
          }
        if ($this->fieldsdata) {
            curl_setopt ($ch, CURLOPT_POST, 1);
            curl_setopt ($ch, CURLOPT_POSTFIELDS, $this->fieldsdata); 
        }

        if (!is_null($this->id) && !is_null($this->pw)) {
            curl_setopt ($ch, CURLOPT_USERPWD, "{$this->id}:{$this->pw}");
        }

        $result = curl_exec ($ch);
        curl_close ($ch);

        list($header, $response) = split("(\r\n){2}",$result, 2);

        $this->_result = $response;
        $headerline = split("\n",$header);
        for ($i=0; $i<count($headerline); $i++) {
            if (ereg("^HTTP", $headerline[$i])) {
                $headerline[$i] = trim($headerline[$i]);
                $httpflag = split(" ",$headerline[$i],3);
                if (!$this->_is_http_200($httpflag)) {
                    return false;
                }
            }
        }
        return true;
    }

    function _is_http_200($httpflag) {
        $this->set_response_code($httpflag[1]);
        if (($httpflag[1] == "200") && ($httpflag[2] == "OK")) {
            return true;  
        }
        else {
            return false;
        }
    }

    function set_response_code($code) {
        $this->response_code = $code;
    } 

    function get_response_code() {
        return $this->response_code;
    } 
}

require_once "curl_ssl.class.php";
$o = new curl;
$o->set_url("https://www.hoge.hoge/index.html");
$o->set_id("user");
$o->set_pw("password");
$o->set_proxy("http://proxy.hoge.com:8080");
$o->set_proxyport("8080");
$o->do_connect();

print $response_code = $o->get_response_code();
print $contents = $o->get_result();


?>