CoolFace
Apppublic

kenken999/php

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
runnerldap.php143 linesDownload Raw Back to classes
1<?php2/**3 * Class for display admin_rights_list4 *5 */6class RunnerLdap7{8	var $ldapconn = null;9	var $ldapbind = null;10 11	var $ldapServerAddress = "";12 13	function __construct( $ldapServerAddress )14	{15		$this->ldapServerAddress = $ldapServerAddress;16	}17	18	/**19	 *20	 */21	function runner_ldap_connect( $usernames, $aPassword, $followRefs)22	{23		// connect to ldap server24		$this->ldapconn = ldap_connect( ldap_getUrl( ldap_getUrl( $this->ldapServerAddress ) ) );25		if ( !$this->ldapconn || ldap_errno($this->ldapconn) != 0 )26			return false;27			28		@ldap_set_option($this->ldapconn, LDAP_OPT_DEBUG_LEVEL, 7);29		@ldap_set_option($this->ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);30		@ldap_set_option($this->ldapconn, LDAP_OPT_REFERRALS, $followRefs ? 1 : 0 );31		32		// binding to ldap server33		foreach($usernames as $u)34		{35			$this->ldapbind = @ldap_bind($this->ldapconn, $u , $aPassword);36			if( $this->ldapbind )37				break;38		}39		40		if( !$this->ldapbind )41			return false;42			43		return true;44	}45	46	/**47	* Search LDAP tree48	* @ An LDAP link identifier, returned by ldap_connect()49	* @ string The search filter 50	* @ array An array of the required attributes51	*/52	function runner_ldap_getData($filter, $baseDN, $attributes = array())53	{54		if( !$attributes )55			$query = ldap_search($this->ldapconn, $baseDN, $filter);56		else 57			$query = ldap_search($this->ldapconn, $baseDN, $filter, $attributes);	58		59		if( $query === FALSE ) 60			return FALSE;61		62		// Read all results from search63		$entries = ldap_get_entries($this->ldapconn, $query);64		return $this->runner_convert_entries($entries);65	}66	67	function runner_convert_entries($aData)68	{	69		$resultArr = array();70		if (!sizeof($aData)) 71			return null;72 73		for ($j = 0; $j < $aData['count']; $j++)74		{75			$result = array();76			foreach ($aData[$j] as $key=>$val)77			{78				if (is_numeric($key))79					continue;80				if(!is_array($val)) 81					$result[$key] = $val;82				83				else if ($val["count"] > 1 || $key == 'memberof') 84				{85					//set array86					$tmpResult = array();87					for ($i=0;$i<$val['count'];$i++)88					{89						$tmpResult[] = $val[$i];90					}91					$result[$key] = $tmpResult;92				}93				else if ($key == 'dn')94					$result[$key] = $val;95				else 96					$result[$key] = $val[0];97			}98			$resultArr[] = $result;99		}100		101		return $resultArr;102	}103	104 105	/**106	 * Convert OU=users,DC=test,DC=xlinesoft,DC=com107	 * to test.xlinesoft.com108	 */109 110	111	function runner_ldap_unbind()112	{113		ldap_unbind($this->ldapconn);114	}115	116	function ldap_error()117	{118		global $dDebug;119		if( $dDebug ) {120			@ldap_get_option($this->ldapconn, LDAP_OPT_DIAGNOSTIC_MESSAGE, $error);121			return $error;122		}123		return ldap_err2str(ldap_errno($this->ldapconn));124	}125	126	127	function getGroupSid($userSid, $primaryGroupId)128	{129		$tgroup = bin2hex(substr($userSid,0, strlen($userSid)-4));130		$group = "";131		for($i = 0; $i<strlen($tgroup); $i += 2)132		{133			$group .= '\\'.substr($tgroup,$i,2);134		}135		$group .= sprintf("\\%02x",$primaryGroupId & 255);136		$group .= sprintf("\\%02x",($primaryGroupId >>8) & 255);137		$group .= sprintf("\\%02x",($primaryGroupId >>16) & 255);138		$group .= sprintf("\\%02x",($primaryGroupId >>24) & 255);139		140		return $group;141	}142}143?>