Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5. Some parts of this website may be subject to other licenses.
Source code for file /joomla/client/ldap.php

Documentation is available at ldap.php

  1. <?php
  2.  
  3. /**
  4. @version        $Id: ldap.php 9764 2007-12-30 07:48:11Z ircmaxell $
  5. @package        Joomla.Framework
  6. @subpackage    Client
  7. @copyright    Copyright (C) 2005 - 2008 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 <pasamio@gmail.com>
  18.  * @package        Joomla.Framework
  19.  * @subpackage    Client
  20.  * @since        1.5
  21.  */
  22. class JLDAP extends JObject
  23. {
  24.     /** @var string Hostname of LDAP server
  25.         @access public */
  26.  
  27.     var $host = null;
  28.     /** @var bool Authorization Method to use
  29.         @access public */
  30.  
  31.     var $auth_method = null;
  32.     /** @var int Port of LDAP server
  33.         @access public */
  34.  
  35.     var $port = null;
  36.     /** @var string Base DN (e.g. o=MyDir)
  37.         @access public */
  38.  
  39.     var $base_dn = null;
  40.     /** @var string User DN (e.g. cn=Users,o=MyDir)
  41.         @access public */
  42.  
  43.     var $users_dn = null;
  44.     /** @var string Search String
  45.         @access public */
  46.  
  47.     var $search_string = null;
  48.     /** @var boolean Use LDAP Version 3
  49.         @access public */
  50.  
  51.     var $use_ldapV3 = null;
  52.     /** @var boolean No referrals (server transfers)
  53.         @access public */
  54.  
  55.     var $no_referrals = null;
  56.     /** @var boolean Negotiate TLS (encrypted communications)
  57.         @access public */
  58.  
  59.     var $negotiate_tls = null;
  60.  
  61.     /** @var string Username to connect to server
  62.         @access public */
  63.  
  64.     var $username = null;
  65.     /** @var string Password to connect to server
  66.         @access public */
  67.  
  68.     var $password = null;
  69.  
  70.     /** @var mixed LDAP Resource Identifier
  71.         @access private */
  72.  
  73.     var $_resource = null;
  74.     /** @var string Current DN
  75.         @access private */
  76.  
  77.     var $_dn = null;
  78.  
  79.     /**
  80.      * Constructor
  81.      *
  82.      * @param object An object of configuration variables
  83.      * @access public
  84.      */
  85.     function __construct($configObj null)
  86.     {
  87.         if (is_object($configObj))
  88.         {
  89.             $vars get_class_vars(get_class($this));
  90.             foreach (array_keys($varsas $var)
  91.             {
  92.                 if (substr($var01!= '_'{
  93.                     if ($param $configObj->get($var)) {
  94.                         $this-> $var $param;
  95.                     }
  96.                 }
  97.             }
  98.         }
  99.     }
  100.  
  101.     /**
  102.      * Connect to server
  103.      * @return boolean True if successful
  104.      * @access public
  105.      */
  106.     function connect()
  107.     {
  108.         if ($this->host == ''{
  109.             return false;
  110.         }
  111.         $this->_resource = ldap_connect($this->host$this->port);
  112.         if ($this->_resource)
  113.         {
  114.             if ($this->use_ldapV3{
  115.                 if (!@ldap_set_option($this->_resourceLDAP_OPT_PROTOCOL_VERSION3)) {
  116.                     return false;
  117.                 }
  118.             }
  119.             if (!@ldap_set_option($this->_resourceLDAP_OPT_REFERRALSintval($this->no_referrals))) {
  120.                 return false;
  121.             }
  122.             if ($this->negotiate_tls{
  123.                 if (!@ldap_start_tls($this->_resource)) {
  124.                     return false;
  125.                 }
  126.             }
  127.             return true;
  128.         else {
  129.             return false;
  130.         }
  131.     }
  132.  
  133.     /**
  134.      * Close the connection
  135.      * @access public
  136.      */
  137.     function close({
  138.         ldap_close($this->_resource);
  139.     }
  140.  
  141.     /**
  142.      * Sets the DN with some template replacements
  143.      *
  144.      * @param string The username
  145.      * @access public
  146.      */
  147.     function setDN($username,$nosub 0)
  148.     {
  149.         if ($this->users_dn == '' || $nosub{
  150.             $this->_dn = $username;
  151.         else if(strlen($username)) {
  152.             $this->_dn = str_replace('[username]'$username$this->users_dn);
  153.         else {
  154.             $this->_dn = '';
  155.         }
  156.     }
  157.  
  158.     /**
  159.      * @return string The current dn
  160.      * @access public
  161.      */
  162.     function getDN({
  163.         return $this->_dn;
  164.     }
  165.  
  166.     /**
  167.      * Anonymously Binds to LDAP Directory
  168.      */
  169.     function anonymous_bind()
  170.     {
  171.         $bindResult @ldap_bind($this->_resource);
  172.         return $bindResult;
  173.     }
  174.  
  175.     /**
  176.      * Binds to the LDAP directory
  177.      *
  178.      * @param string The username
  179.      * @param string The password
  180.      * @return boolean Result
  181.      * @access public
  182.      */
  183.     function bind($username null$password null$nosub 0)
  184.     {
  185.         if (is_null($username)) {
  186.             $username $this->username;
  187.         }
  188.         if (is_null($password)) {
  189.             $password $this->password;
  190.         }
  191.         $this->setDN($username,$nosub);
  192.         //if(strlen($this->getDN()))
  193.         $bindResult @ldap_bind($this->_resource$this->getDN()$password);
  194.         return $bindResult;
  195.     }
  196.  
  197.     /**
  198.      * Perform an LDAP search using comma seperated search strings
  199.      *
  200.      * @param string search string of search values
  201.      */
  202.     function simple_search($search)
  203.     {
  204.         $results explode(';'$search);
  205.         foreach($results as $key=>$result{
  206.             $results[$key'('.$result.')';
  207.         }
  208.         return $this->search($results);
  209.     }
  210.  
  211.  
  212.     /**
  213.      * Perform an LDAP search
  214.      *
  215.      * @param array Search Filters (array of strings)
  216.      * @param string DN Override
  217.      * @return array Multidimensional array of results
  218.      * @access public
  219.      */
  220.     function search($filters$dnoverride null)
  221.     {
  222.         $attributes array ();
  223.         if ($dnoverride{
  224.             $dn $dnoverride;
  225.         else {
  226.             $dn $this->base_dn;
  227.         }
  228.  
  229.         $resource $this->_resource;
  230.  
  231.         foreach ($filters as $search_filter)
  232.         {
  233.             $search_result @ldap_search($resource$dn$search_filter);
  234.             if ($search_result && ($count @ldap_count_entries($resource$search_result)) 0)
  235.             {
  236.                 for ($i 0$i $count$i++)
  237.                 {
  238.                     $attributes[$iArray ();
  239.                     if (!$i{
  240.                         $firstentry ldap_first_entry($resource$search_result);
  241.                     else {
  242.                         $firstentry ldap_next_entry($resource$firstentry);
  243.                     }
  244.                     $attributes_array ldap_get_attributes($resource$firstentry)// load user-specified attributes
  245.                     // ldap returns an array of arrays, fit this into attributes result array
  246.                     foreach ($attributes_array as $ki => $ai)
  247.                     {
  248.                         if (is_array($ai))
  249.                         {
  250.                             $subcount $ai['count'];
  251.                             $attributes[$i][$kiArray ();
  252.                             for ($k 0$k $subcount$k++{
  253.                                 $attributes[$i][$ki][$k$ai[$k];
  254.                             }
  255.                         }
  256.                     }
  257.                     $attributes[$i]['dn'ldap_get_dn($resource$firstentry);
  258.                 }
  259.             }
  260.         }
  261.         return $attributes;
  262.     }
  263.  
  264.     /**
  265.      * Replace an entry and return a true or false result
  266.      *
  267.      * @param string dn The DN which contains the attribute you want to replace
  268.      * @param string attribute The attribute values you want to replace
  269.      * @return mixed result of comparison (true, false, -1 on error)
  270.      */
  271.  
  272.     function replace($dn$attribute{
  273.         return ldap_mod_replace($this->_resource$dn$attribute);
  274.     }
  275.  
  276.  
  277.     /**
  278.      * Modifies an entry and return a true or false result
  279.      *
  280.      * @param string dn The DN which contains the attribute you want to modify
  281.      * @param string attribute The attribute values you want to modify
  282.      * @return mixed result of comparison (true, false, -1 on error)
  283.      */
  284.     function modify($dn$attribute{
  285.         return ldap_modify($this->_resource$dn$attribute);
  286.     }
  287.  
  288.     /**
  289.      * Removes attribute value from given dn and return a true or false result
  290.      *
  291.      * @param string dn The DN which contains the attribute you want to remove
  292.      * @param string attribute The attribute values you want to remove
  293.      * @return mixed result of comparison (true, false, -1 on error)
  294.      */
  295.     function remove($dn$attribute)
  296.     {
  297.         $resource $this->_resource;
  298.         return ldap_mod_del($resource$dn$attribute);
  299.     }
  300.  
  301.     /**
  302.      * Compare an entry and return a true or false result
  303.      *
  304.      * @param string dn The DN which contains the attribute you want to compare
  305.      * @param string attribute The attribute whose value you want to compare
  306.      * @param string value The value you want to check against the LDAP attribute
  307.      * @return mixed result of comparison (true, false, -1 on error)
  308.      */
  309.     function compare($dn$attribute$value{
  310.         return ldap_compare($this->_resource$dn$attribute$value);
  311.     }
  312.  
  313.     /**
  314.      * Read all or specified attributes of given dn
  315.      *
  316.      * @param string dn The DN of the object you want to read
  317.      * @param string attribute The attribute values you want to read (Optional)
  318.      * @return array of attributes or -1 on error
  319.      */
  320.     function read($dn$attribute array())
  321.     {
  322.         $base substr($dn,strpos($dn,',')+1);
  323.         $cn substr($dn,0,strpos($dn,','));
  324.         $result ldap_read($this->_resource$base$cn);
  325.  
  326.         if ($result{
  327.             // TODO: instead of just returning array of attributes, convert to object before returning
  328.             return ldap_get_entries($this->_resource$result);
  329.         else {
  330.             return $result;
  331.         }
  332.     }
  333.  
  334.     /**
  335.      * Converts a dot notation IP address to net address (e.g. for Netware, etc)
  336.      *
  337.      * @param string IP Address (e.g. xxx.xxx.xxx.xxx)
  338.      * @return string Net address
  339.      * @access public
  340.      */
  341.     function ipToNetAddress($ip)
  342.     {
  343.         $parts explode('.'$ip);
  344.         $address '1#';
  345.  
  346.         foreach ($parts as $int{
  347.             $tmp dechex($int);
  348.             if (strlen($tmp!= 2{
  349.                 $tmp '0' $tmp;
  350.             }
  351.             $address .= '\\' $tmp;
  352.         }
  353.         return $address;
  354.     }
  355.  
  356.     /**
  357.      * extract readable network address from the LDAP encoded networkAddress attribute.
  358.      * @author Jay Burrell, Systems & Networks, Mississippi State University
  359.      *  Please keep this document block and author attribution in place.
  360.      *
  361.      *   Novell Docs, see: http://developer.novell.com/ndk/doc/ndslib/schm_enu/data/sdk5624.html#sdk5624
  362.      *   for Address types: http://developer.novell.com/ndk/doc/ndslib/index.html?page=/ndk/doc/ndslib/schm_enu/data/sdk4170.html
  363.      *   LDAP Format, String:
  364.      *      taggedData = uint32String "#" octetstring
  365.      *      byte 0 = uint32String = Address Type: 0= IPX Address; 1 = IP Address
  366.      *      byte 1 = char = "#" - separator
  367.      *      byte 2+ = octetstring - the ordinal value of the address
  368.      *    Note: with eDirectory 8.6.2, the IP address (type 1) returns
  369.      *                  correctly, however, an IPX address does not seem to.  eDir 8.7 may correct this.
  370.      *   Enhancement made by Merijn van de Schoot:
  371.      *      If addresstype is 8 (UDP) or 9 (TCP) do some additional parsing like still returning the IP address
  372.      *      TODO: Return an extra value with UDP or TCP portnumber
  373.      */
  374.     function LDAPNetAddr($networkaddress)
  375.     {
  376.         $addr "";
  377.         $addrtype intval(substr($networkaddress01));
  378.         $networkaddress substr($networkaddress2)// throw away bytes 0 and 1 which should be the addrtype and the "#" separator
  379.  
  380.         if (($addrtype == 8|| ($addrtype 9)) {    // if udp or tcp, (TODO fill addrport and) strip portnumber information from address
  381.             $networkaddress substr($networkaddress(strlen($networkaddress)-4));
  382.         }
  383.  
  384.         $addrtypes array (
  385.             'IPX',
  386.             'IP',
  387.             'SDLC',
  388.             'Token Ring',
  389.             'OSI',
  390.             'AppleTalk',
  391.             'NetBEUI',
  392.             'Socket',
  393.             'UDP',
  394.             'TCP',
  395.             'UDP6',
  396.             'TCP6',
  397.             'Reserved (12)',
  398.             'URL',
  399.             'Count'
  400.         );
  401.         $len strlen($networkaddress);
  402.         if ($len 0)
  403.         {
  404.             for ($i 0$i $len$i += 1)
  405.             {
  406.                 $byte substr($networkaddress$i1);
  407.                 $addr .= ord($byte);
  408.                 if ( ($addrtype == 1|| ($addrtype == 8|| ($addrtype 9) ) // dot separate IP addresses...
  409.                     $addr .= ".";
  410.                 }
  411.             }
  412.             if ( ($addrtype == 1|| ($addrtype == 8|| ($addrtype 9) ) // strip last period from end of $addr
  413.                 $addr substr($addr0strlen($addr1);
  414.             }
  415.         else {
  416.             $addr .= "address not available.";
  417.         }
  418.         return Array('protocol'=>$addrtypes[$addrtype]'address'=>$addr);
  419.     }
  420. }

Documentation generated on Tue, 29 Jan 2008 18:48:09 +0000 by phpDocumentor 1.3.1