Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/client/ldap.php

Documentation is available at ldap.php

  1. <?php
  2.  
  3. /**
  4. @version        $Id: ldap.php 6704 2007-02-23 11:16:43Z pasamio $
  5. @package        Joomla.Framework
  6. @subpackage    Client
  7. @copyright    Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
  8. @license        GNU/GPL, see LICENSE.php
  9. *  Joomla! is free software and parts of it may contain or be derived from the
  10. *  GNU General Public License or other free or open source software licenses.
  11. *  See COPYRIGHT.php for copyright notices and details.
  12. */
  13.  
  14. /**
  15.  * LDAP client class
  16.  *
  17.  * @author        Samuel Moffatt <[email protected]>
  18.  * @package        Joomla.Framework
  19.  * @subpackage        Client
  20.  * @since        1.5
  21.  */
  22. class JLDAP {
  23.     /** @var string Hostname of LDAP server
  24.         @access public */
  25.  
  26.     var $host = null;
  27.     /** @var bool Authorization Method to use
  28.         @access public */
  29.  
  30.     var $auth_method = null;
  31.     /** @var int Port of LDAP server
  32.         @access public */
  33.  
  34.     var $port = null;
  35.     /** @var string Base DN (e.g. o=MyDir)
  36.         @access public */
  37.  
  38.     var $base_dn = null;
  39.     /** @var string User DN (e.g. cn=Users,o=MyDir)
  40.         @access public */
  41.  
  42.     var $users_dn = null;
  43.     /** @var string Search String
  44.         @access public */
  45.  
  46.     var $search_string = null;
  47.     /** @var boolean Use LDAP Version 3
  48.         @access public */
  49.  
  50.     var $use_ldapV3 = null;
  51.     /** @var boolean No referrals (server transfers)
  52.         @access public */
  53.  
  54.     var $no_referrals = null;
  55.     /** @var boolean Negotiate TLS (encrypted communications)
  56.         @access public */
  57.  
  58.     var $negotiate_tls = null;
  59.  
  60.     /** @var string Username to connect to server
  61.         @access public */
  62.  
  63.     var $username = null;
  64.     /** @var string Password to connect to server
  65.         @access public */
  66.  
  67.     var $password = null;
  68.  
  69.     /** @var mixed LDAP Resource Identifier
  70.         @access private */
  71.  
  72.     var $_resource = null;
  73.     /** @var string Current DN
  74.         @access private */
  75.  
  76.     var $_dn = null;
  77.  
  78.     /**
  79.      * Constructor
  80.      * @param object An object of configuration variables
  81.      * @access public
  82.      */
  83.     function JLDAP($configObj null{
  84.         if (is_object($configObj)) {
  85.             $vars get_class_vars(get_class($this));
  86.             foreach (array_keys($varsas $var{
  87.                 if (substr($var01!= '_'{
  88.                     if ($param $configObj->get($var)) {
  89.                         $this-> $var $param;
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.     }
  95.  
  96.     /**
  97.      * Connect to server
  98.      * @return boolean True if successful
  99.      * @access public
  100.      */
  101.     function connect({
  102.         if ($this->host == ''{
  103.             return false;
  104.         }
  105.         $this->_resource = ldap_connect($this->host$this->port);
  106.         if ($this->_resource{
  107.             if ($this->use_ldapV3{
  108.                 if (!ldap_set_option($this->_resourceLDAP_OPT_PROTOCOL_VERSION3)) {
  109.                     return false;
  110.                 }
  111.             }
  112.             if (!ldap_set_option($this->_resourceLDAP_OPT_REFERRALSintval($this->no_referrals))) {
  113.                 return false;
  114.             }
  115.             if ($this->negotiate_tls{
  116.                 if (!ldap_start_tls($this->_resource)) {
  117.                     return false;
  118.                 }
  119.             }
  120.             return true;
  121.         else {
  122.             return false;
  123.         }
  124.     }
  125.  
  126.     /**
  127.      * Close the connection
  128.      * @access public
  129.      */
  130.     function close({
  131.         ldap_close($this->_resource);
  132.     }
  133.  
  134.     /**
  135.      * Sets the DN with some template replacements
  136.      * @param string The username
  137.      * @access public
  138.      */
  139.     function setDN($username,$nosub=0{
  140.         if ($this->users_dn == '' || $nosub{
  141.             $this->_dn = $username;
  142.         else {
  143.             $this->_dn = str_replace('[username]'$username$this->users_dn);
  144.         }
  145.     }
  146.  
  147.     /**
  148.      * @return string The current dn
  149.      * @access public
  150.      */
  151.     function getDN({
  152.         return $this->_dn;
  153.     }
  154.  
  155.     /**
  156.      * Anonymously Binds to LDAP Directory
  157.      */
  158.     function anonymous_bind({
  159.         $bindResult @ldap_bind($this->_resource);
  160.         return $bindResult;
  161.     }
  162.  
  163.     /**
  164.      * Binds to the LDAP directory
  165.      * @param string The username
  166.      * @param string The password
  167.      * @return boolean Result
  168.      * @access public
  169.      */
  170.     function bind($username null$password null$nosub 0{
  171.         if (is_null($username)) {
  172.             $username $this->username;
  173.         }
  174.         if (is_null($password)) {
  175.             $password $this->password;
  176.         }
  177.         $this->setDN($username,$nosub);
  178.         $bindResult ldap_bind($this->_resource$this->getDN()$password);
  179.         return $bindResult;
  180.     }
  181.  
  182.     /**
  183.      * Perform an LDAP search using comma seperated search strings
  184.      * @param string search string of search values
  185.      */
  186.     function simple_search($search{
  187.         $results explode(';'$search);
  188.         foreach($results as $key=>$result{
  189.             $results[$key'('.$result.')';
  190.         }
  191.         return $this->search($results);
  192.     }
  193.  
  194.  
  195.     /**
  196.      * Perform an LDAP search
  197.      * @param array Search Filters (array of strings)
  198.      * @param string DN Override
  199.      * @return array Multidimensional array of results
  200.      * @access public
  201.      */
  202.     function search($filters$dnoverride null{
  203.         $attributes array ();
  204.         if ($dnoverride{
  205.             $dn $dnoverride;
  206.         else {
  207.             $dn $this->base_dn;
  208.         }
  209.  
  210.         $resource $this->_resource;
  211.  
  212.         foreach ($filters as $search_filter{
  213.             $search_result ldap_search($resource$dn$search_filter);
  214.             if ($search_result && ($count ldap_count_entries($resource$search_result)) 0{
  215.                 for ($i 0$i $count$i++{
  216.                     $attributes[$iArray ();
  217.                     if (!$i{
  218.                         $firstentry ldap_first_entry($resource$search_result);
  219.                     else {
  220.                         $firstentry ldap_next_entry($resource$firstentry);
  221.                     }
  222.                     $attributes_array ldap_get_attributes($resource$firstentry)// load user-specified attributes
  223.                     // ldap returns an array of arrays, fit this into attributes result array
  224.                     foreach ($attributes_array as $ki => $ai{
  225.                         if (is_array($ai)) {
  226.                             $subcount $ai['count'];
  227.                             $attributes[$i][$kiArray ();
  228.                             for ($k 0$k $subcount$k++{
  229.                                 $attributes[$i][$ki][$k$ai[$k];
  230.                             }
  231.                         }
  232.                     }
  233.                     $attributes[$i]['dn'ldap_get_dn($resource$firstentry);
  234.                 }
  235.             }
  236.         }
  237.         return $attributes;
  238.     }
  239.  
  240.     /**
  241.      * Compare an entry and return a true or false result
  242.      * @param string dn The DN which contains the attribute you want to compare
  243.      * @param string attribute The attribute whose value you want to compare
  244.      * @param string value The value you want to check against the LDAP attribute
  245.      * @return mixed result of comparison (true, false, -1 on error)
  246.      */
  247.     function compare($dn$attribute$value{
  248.         return ldap_compare($this->_resource$dn$attribute$value);
  249.     }
  250.  
  251.     /**
  252.      * Converts a dot notation IP address to net address (e.g. for Netware, etc)
  253.      * @param string IP Address (e.g. xxx.xxx.xxx.xxx)
  254.      * @return string Net address
  255.      * @access public
  256.      */
  257.     function ipToNetAddress($ip{
  258.         $parts explode('.'$ip);
  259.         $address '1#';
  260.  
  261.         foreach ($parts as $int{
  262.             $tmp dechex($int);
  263.             if (strlen($tmp!= 2{
  264.                 $tmp '0' $tmp;
  265.             }
  266.             $address .= '\\' $tmp;
  267.         }
  268.         return $address;
  269.     }
  270.  
  271.     /**
  272.      * extract readable network address from the LDAP encoded networkAddress attribute.
  273.      * @author Jay Burrell, Systems & Networks, Mississippi State University
  274.      *  Please keep this document block and author attribution in place.
  275.      *   Novell Docs, see: http://developer.novell.com/ndk/doc/ndslib/schm_enu/data/sdk5624.html#sdk5624
  276.      *   for Address types: http://developer.novell.com/ndk/doc/ndslib/index.html?page=/ndk/doc/ndslib/schm_enu/data/sdk4170.html
  277.      *   LDAP Format, String:
  278.      *      taggedData = uint32String "#" octetstring
  279.      *      byte 0 = uint32String = Address Type: 0= IPX Address; 1 = IP Address
  280.      *      byte 1 = char = "#" - separator
  281.      *      byte 2+ = octetstring - the ordinal value of the address
  282.      *    Note: with eDirectory 8.6.2, the IP address (type 1) returns
  283.      *                  correctly, however, an IPX address does not seem to.  eDir 8.7 may
  284.      *                 correct this.
  285.      */
  286.     function LDAPNetAddr($networkaddress{
  287.         $addr "";
  288.         $addrtype intval(substr($networkaddress01));
  289.         $networkaddress substr($networkaddress2)// throw away bytes 0 and 1 which should be the addrtype and the "#" separator
  290.         $addrtypes array (
  291.             'IPX',
  292.             'IP',
  293.             'SDLC',
  294.             'Token Ring',
  295.             'OSI',
  296.             'AppleTalk',
  297.             'NetBEUI',
  298.             'Socket',
  299.             'UDP',
  300.             'TCP',
  301.             'UDP6',
  302.             'TCP6',
  303.             'Reserved (12)',
  304.             'URL',
  305.             'Count'
  306.         );
  307.         $len strlen($networkaddress);
  308.         if ($len 0{
  309.             for ($i 0$i $len$i += 1{
  310.                 $byte substr($networkaddress$i1);
  311.                 $addr .= ord($byte);
  312.                 if ($addrtype == 1// dot separate IP addresses...
  313.                     $addr .= ".";
  314.                 }
  315.             }
  316.             if ($addrtype == 1// strip last period from end of $addr
  317.                 $addr substr($addr0strlen($addr1);
  318.             }
  319.         else {
  320.             $addr .= "address not available.";
  321.         }
  322.         return Array('protocol'=>$addrtypes[$addrtype]'address'=>$addr);
  323.     }
  324. }
  325. ?>

Documentation generated on Mon, 05 Mar 2007 21:09:25 +0000 by phpDocumentor 1.3.1