1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
<?php
class PhammLdap
{
/**
* Open the LDAP connection
*
* @author Alessandro De Zorzi <adezorzi AT rhx DOT it>
**/
static function phamm_connect ()
{
// Open LDAP connection to server
$connect = @ldap_connect(LDAP_HOST_NAME,LDAP_PORT)
or die ("LDAP connection Failed!");
ldap_set_option($connect,LDAP_OPT_PROTOCOL_VERSION,LDAP_PROTOCOL_VERSION);
// Start TLS session
if (LDAP_TLS == 1)
{
@ldap_start_tls($connect)
or die ("Could not start TLS. Please check your LDAP server configuration.");
}
return $connect;
}
/**
* Generic LDAP Single-level search
*
* @author Alessandro De Zorzi <adezorzi@rhx.it>
*
* @todo add attrsonly, sizelimit, timelimit
*
* @param string $base_dn
* @param string $filter
* @param array $attributes
* @param array $attributes
* @param array $short Sort Attributes
**/
static function phamm_list ($base_dn,$filter,$attributes=null,$sort=null)
{
global $connect;
// Do a LDAP search
if ($attributes)
$search = ldap_list($connect,$base_dn,$filter,$attributes);
else
$search = ldap_list($connect,$base_dn,$filter);
// Order the results if possible
if (version_compare(phpversion(), "4.2.0", ">="))
ldap_sort($connect,$search,$sort);
// Get entries
$entries = ldap_get_entries($connect, $search);
// Free the memory
ldap_free_result($search);
// Return the entry
return $entries;
}
/**
* Generic LDAP search
*
* @author Alessandro De Zorzi <adezorzi@rhx.it>
*
* @todo add attrsonly, sizelimit, timelimit
*
* @param string $base_dn
* @param string $filter
* @param array $attributes
* @return array $entries
**/
static function phamm_search ($base_dn,$filter,$attributes=null,$sort=null)
{
global $connect;
// Do a LDAP search
if (isset($attributes))
$search = ldap_search($connect,$base_dn,$filter,$attributes);
else
$search = ldap_search($connect,$base_dn,$filter);
// Order the results if possible
if (version_compare(phpversion(), "4.2.0", ">="))
ldap_sort($connect, $search,$sort);
// Get entries
$entries = ldap_get_entries($connect, $search);
// Free the memory
ldap_free_result($search);
// Return the entry
return $entries;
}
/**
* Create new LDAP entry
*
* @author Alessandro De Zorzi <adezorzi@rhx.it>
*
* @param string $dn
* @param array $entry The attributes info
* @return bool $r
**/
static function phamm_add ($dn,$entry)
{
global $connect;
$r = ldap_add ($connect, $dn, $entry);
return $r;
}
/**
* Modify a LDAP entry
*
* @author Alessandro De Zorzi <adezorzi@rhx.it>
*
* @param string $dn
* @param array $entry The attributes info
* @return bool $r
**/
static function phamm_modify ($dn,$entry)
{
global $connect;
$r = ldap_modify ($connect, $dn, $entry );
return $r;
}
/**
* Get the values of a DN
*
* @author Alessandro De Zorzi <adezorzi@rhx.it>
*
* @param string $dn
* @param string $filter
* @return array $results
**/
static function phamm_self_values ($dn, $filter="(cn=*)")
{
global $connect;
$search = ldap_search($connect,$dn,$filter);
$results = ldap_get_entries($connect, $search);
return $results;
}
/**
* Add new attribute (multiple)
*
* @author Alessandro De Zorzi <adezorzi@rhx.it>
*
* @param string $dn
* @param array $entry The attributes info
* @return bool $r
**/
static function phamm_mod_add ($dn,$entry)
{
global $connect;
$r = ldap_mod_add ($connect, $dn, $entry);
return $r;
}
/**
* Delete attribute (multiple)
*
* @author Alessandro De Zorzi <adezorzi@rhx.it>
*
* @param string $dn
* @param array $entry The attributes info
* @return bool $r
**/
static function phamm_mod_del ($dn,$entry)
{
global $connect;
$r = ldap_mod_del ($connect, $dn, $entry);
return $r;
}
/**
* Delete LDAP entry recursive
*
* @author gabriel at hrz dot uni-marburg dot de
* http://it2.php.net/manual/it/static function.ldap-delete.php
*
* @param $ds
* @param string $dn The DN
* @param bool $recursive
* @return bool
**/
static function phamm_delete($dn,$recursive=false)
{
global $connect;
if($recursive == false)
{
return(ldap_delete($connect,$dn));
}
else
{
//searching for sub entries
$sr=ldap_list($connect,$dn,"ObjectClass=*",array(""));
$info = ldap_get_entries($connect, $sr);
for($i=0; $i<$info['count']; $i++)
{
//deleting recursively sub entries
$result=myldap_delete($connect,$info[$i]['dn'],$recursive);
if(!$result)
{
//return result code, if delete fails
return($result);
}
}
return(ldap_delete($connect,$dn));
}
}
/**
* Simple LDAP error
**/
static function phamm_error ()
{
global $connect;
return _("LDAP Error: ").ldap_error($connect).' ('._("Code ").ldap_errno($connect).')';
}
//
}
|