1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189 |
- <?php
- // Update knei: impüortuser um objectGUID erweitert (analog SID angelegt)
- /*
- Examples:
- include 'myldap.php';
- $myldap = new MyLDAP;
- $myldap->Login("praes", "vortrag"); -> TRUE
- $myldap->Login("preas", "vortrag"); -> FALSE
- $myldap->ImportUsers(); -> Import Users from LDAP to MySQL
- $myldap->GetSid($username); -> Get SID from Username
- $myldap->GetObjectGUID($username); -> Get object from Username (hex as str whitout separators)
- $myldap->GetUsername($sid); -> Get Username from SID
- $myldap->GetGroups($username); -> Get groups (comma separated) // username or sid
- $myldap->IsUserInGroup($username, $groupname); return true or false
- $myldap->GetUserInformation($username); -> will return an array // username, displayname or sid is allowed
- array: $user["sid"] $user["username"] $user["email"] $user["displayname"] $user["description"] $user["title"] $user["forename"] $user["surname"]
- $user["room"] $user["department"] $user["departmentnumber"] $user["telephone"] $user["mobilephone"] $user["manager"] $user["groups"]
- $myldap->$myldap->GetUsersFromGroup("group"); -> will return an array with usernames
- */
- class MyLDAP
- {
- // Config
- protected $ldaphost = "ads.iwmh.fraunhofer.de";
- protected $ldapuser = "unix";
- protected $ldappasswd = "tunix+-";
- protected $ldapbasedn = "CN=Users,DC=iwmh,DC=fraunhofer,DC=de";
- protected $mysqlhost = "localhost";
- protected $mysqldb = "adsiwmh";
- protected $mysqluser = "unix";
- protected $mysqlpasswd = "tunix+-";
- protected $ignoreUsers = "Administrator,opsihelper,mailer,unix,ASPNET,netapp-ad,ILS_ANONYMOUS_US,IWAM_SRV-ADS1,standard,Gast,dirxml-user,pcpatch,orgadmin,blackberry,Johnson.Controls,olat-admin,pwdsync,bereitschaft,SP_Dienste,Xpression";
- public function Login($username, $passwd)
- {
- $ldaphost = $this->ldaphost;
- $ldapbasedn = $this->ldapbasedn;
- if ( ($username == "") || ($username == "NULL") || ($username == null)) return false;
- $attribut = array("displayname", "sAMAccountName");
- $username = str_replace("IWMH\\", "", $username);
- $username = str_replace("Iwmh\\", "", $username);
- $ldapuser = $username."@iwmh.fhg.de";
- $ldappasswd = $passwd;
- $ldapquery = "(&(objectClass=Person)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(sAMAccountName=$username))";
- $ds = ldap_connect("LDAP://".$ldaphost);
- if (!$ds) exit ("Verbindung zum LDAP Server nicht m?glich");
- if (!ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3)) exit ("LDAP: Kann das Protokoll nicht auf Version 3 setzen");
- $ldapbind = ldap_bind($ds, $ldapuser, $ldappasswd);
- if (!$ldapbind) return false;
- $sr = ldap_search($ds, $ldapbasedn, $ldapquery);
- if (!$sr) exit ("LDAP search fehlgeschlagen...");
- for ($entry=ldap_first_entry($ds,$sr), $i=0; $entry!=false; $entry=ldap_next_entry($ds,$entry), $i++)
- {
- for ($j=0; $j<count($attribut); $j++)
- {
- $tmp = ldap_get_values($ds, $entry, $attribut[$j]);
- $info[$i][$j] = $tmp[0];
- }
- }
- ldap_close($ds);
- for ($i=0; $i<count($info); $i++)
- {
- if ((strcasecmp($info[$i][1], $username) == 0))
- return true;
- }
- return false;
- }
- public function GetSid($username)
- {
- if ($username == "") return "";
- $ldaphost = $this->ldaphost;
- $ldapbasedn = $this->ldapbasedn;
- $ldapuser = $this->ldapuser;
- $ldappasswd = $this->ldappasswd;
- // echo "GetSid:".$username."\n";
- $ds = ldap_connect("LDAP://".$ldaphost);
- ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
- ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
- $ldapbind = ldap_bind($ds, $ldapuser, $ldappasswd);
- if (!$ldapbind) return "ERROR";
- // echo $username."\n";
- $sr = ldap_search($ds, $ldapbasedn, "(sAMAccountName=".$username.")");
- $entries = ldap_get_entries($ds, $sr);
- if (is_bool($sr)) echo "GetSid:".$username."\n";
- if (count($entries) == 2)
- {
- // All SID's begin with S-
- $sid = "S-"; // Convert Bin to Hex and split into byte chunks
- $sidinhex = str_split(bin2hex($entries[0]['objectsid'][0]), 2); // Byte 0 = Revision Level
- if (count($sidinhex) == 28)
- {
- $sid = $sid.hexdec($sidinhex[0])."-"; // Byte 1-7 = 48 Bit Authority
- $sid = $sid.hexdec($sidinhex[6].$sidinhex[5].$sidinhex[4].$sidinhex[3].$sidinhex[2].$sidinhex[1]); // Byte 8 count of sub authorities - Get number of sub-authorities
- $subauths = hexdec($sidinhex[7]); //Loop through Sub Authorities
- for($i = 0; $i < $subauths; $i++)
- {
- $start = 8 + (4 * $i); // X amount of 32Bit (4 Byte) Sub Authorities
- $sid = $sid."-".hexdec($sidinhex[$start+3].$sidinhex[$start+2].$sidinhex[$start+1].$sidinhex[$start]);
- }
- }
- }
- else
- return "S-0-0";
- return $sid;
- }
- public function getObjectGUID($username)
- {
- if ($username == "") return "";
- $ldaphost = $this->ldaphost;
- $ldapbasedn = $this->ldapbasedn;
- $ldapuser = $this->ldapuser;
- $ldappasswd = $this->ldappasswd;
- $ds = ldap_connect("LDAP://".$ldaphost);
- ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
- ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
- $ldapbind = ldap_bind($ds, $ldapuser, $ldappasswd);
- if (!$ldapbind) return "ERROR";
- $sr = ldap_search($ds, $ldapbasedn, "(sAMAccountName=".$username.")");
- $entries = ldap_first_entry($ds, $sr);
- $object_guid = ldap_get_values_len($ds, $entries, 'objectGUID');
- if(count($object_guid) ==2)
- $result = strtoupper(bin2hex($object_guid[0]));
- else
- $result = "0";
- return $result;
- }
- public function GetwhenCreated($username)
- {
- if ($username == "") return "";
- $ldaphost = $this->ldaphost;
- $ldapbasedn = $this->ldapbasedn;
- $ldapuser = $this->ldapuser;
- $ldappasswd = $this->ldappasswd;
- $ds = ldap_connect("LDAP://".$ldaphost);
- ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
- ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
- $ldapbind = ldap_bind($ds, $ldapuser, $ldappasswd);
- if (!$ldapbind) return "ERROR";
- $sr = ldap_search($ds, $ldapbasedn, "(sAMAccountName=".$username.")");
- $entries = ldap_first_entry($ds, $sr);
- $whenCreated = ldap_get_values($ds, $entries, 'whenCreated');
- if(count($whenCreated) ==2)
- {
- $timeStrInUTC = preg_replace("/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2}).+/","$1-$2-$3 $4:$5:$6", $whenCreated[0]);
- $timeInUTC = strtotime($timeStrInUTC.' UTC');
- $result = date("Y-m-d H:i:s", $timeInUTC);
- }
- else
- $result = "0";
- return $result;
- }
- private function Ldap_Query($ldapquery, $attribut)
- {
- $ldaphost = $this->ldaphost;
- $ldapbasedn = $this->ldapbasedn;
- $ldapuser = $this->ldapuser;
- $ldappasswd = $this->ldappasswd;
- $ds = ldap_connect("LDAP://".$ldaphost);
- $ldapbind = ldap_bind($ds, $ldapuser, $ldappasswd);
- $sr = ldap_search($ds, $ldapbasedn, $ldapquery);
- $i=0;
- $j=0;
- for ($entry=ldap_first_entry($ds,$sr), $i=0; $entry!=false; $entry=ldap_next_entry($ds,$entry), $i++)
- {
- $test = ldap_get_attributes($ds, $entry);
- for ($j=0; $j<count($attribut); $j++)
- {
- if(array_search($attribut[$j], $test))
- $tmp = ldap_get_values($ds, $entry, $attribut[$j]);
- else
- $tmp = array("");
- $ldap_result[$i][$attribut[$j]] = ""; // set array
- for($k=0; $k<count($tmp); $k++)
- {
- if (isset($tmp[$k])) $ldap_result[$i][$attribut[$j]] .= $tmp[$k];
- }
- $ldap_result[$i][$attribut[$j]] = str_replace("CN=Builtin,DC=iwmh,DC=fraunhofer,DC=de", "", $ldap_result[$i][$attribut[$j]]); // trim groups
- $ldap_result[$i][$attribut[$j]] = str_replace($ldapbasedn, "", $ldap_result[$i][$attribut[$j]]); // trim groups
- $ldap_result[$i][$attribut[$j]] = str_replace(",CN=", ";", $ldap_result[$i][$attribut[$j]]); // trim groups
- $ldap_result[$i][$attribut[$j]] = str_replace("CN=", "", $ldap_result[$i][$attribut[$j]]); // trim groups
- $ldap_result[$i][$attribut[$j]] = str_replace("\,", ",", $ldap_result[$i][$attribut[$j]]); // trim displaynames
- $ldap_result[$i][$attribut[$j]] = rtrim($ldap_result[$i][$attribut[$j]], ",");
- //echo $attribut[$j]." - ".$ldap_result[$i][$attribut[$j]]."<br>";
- }
- }
- ldap_close($ds);
- return $ldap_result;
- }
- private function MySQLConnect()
- {
- $mysqlhost = $this->mysqlhost;
- $mysqldb = $this->mysqldb;
- $mysqluser = $this->mysqluser;
- $mysqlpasswd = $this->mysqlpasswd;
- $mysql = mysql_connect($mysqlhost, $mysqluser, $mysqlpasswd) or die("Keine MySQL-Verbindung: " . mysql_error());
- mysql_select_db($mysqldb, $mysql) or die("Auswahl der Datenbank fehlgeschlagen!"."<br>". mysql_error()."<br>DB:".$mysqldb);
- mysql_query("SET NAMES 'utf8'", $mysql);
- mysql_query("SET CHARACTER SET 'utf8'", $mysql);
- return $mysql;
- }
- private function CreateMySQLUserTable($ldap)
- {
- // $apos = "'"; Apostroph ersetzen
- $apos = "\'";
- $mysql = $this->MySQLConnect();
- $ignoreUsers = explode(",",$this->ignoreUsers);
- $error = true;
- mysql_query("TRUNCATE adsuser;", $mysql); // Delete Data
- for($i=0; $i<count($ldap); $i++)
- {
- $import = true;
- if("" == $ldap[$i]["sAMAccountName"]) $import = false;
- if("" == $ldap[$i]["sid"]) $import = false;
- for($j=0; $j < count($ignoreUsers); $j++)
- {
- if($ignoreUsers[$j] == $ldap[$i]["sAMAccountName"]) $import = false;
- }
- if ($import == true)
- {
- $sqlquery = "INSERT INTO `adsuser` (`sid`, `username`, `fhgusername`, `email`, `forename`, `surname`, `room`, `institute`, `department`, `departmentnumber`, `telephone`, `fax`, `mobilephone`, `displayname`,`groups`, `manager`, `description`,`title`, `employeeid`, `employeetype`, `accountexpires`, `lastlogin`, `whencreated`, `objectGUID`) VALUES";
- $sqlquery .= "('".$ldap[$i]["sid"]."', ";
- $sqlquery .= "'".strtolower($ldap[$i]["sAMAccountName"])."', ";
- $sqlquery .= "'".$ldap[$i]["extensionAttribute6"]."', ";
- $sqlquery .= "'".strtolower(str_replace("'", $apos, $ldap[$i]["mail"]))."', ";
- $sqlquery .= "'".str_replace("'", $apos, $ldap[$i]["givenName"])."', ";
- $sqlquery .= "'".str_replace("'", $apos, $ldap[$i]["sn"])."', ";
- $sqlquery .= "'".$ldap[$i]["physicalDeliveryOfficeName"]."', ";
- $sqlquery .= "'".$ldap[$i]["extensionAttribute5"]."', ";
- $sqlquery .= "'".$ldap[$i]["department"]."', ";
- $sqlquery .= "'".$ldap[$i]["departmentNumber"]."', ";
- $sqlquery .= "'".$ldap[$i]["telephoneNumber"]."', ";
- $sqlquery .= "'".$ldap[$i]["facsimileTelephoneNumber"]."', ";
- $sqlquery .= "'".$ldap[$i]["mobile"]."', ";
- $sqlquery .= "'".str_replace("'", $apos, $ldap[$i]["displayName"])."', ";
- $sqlquery .= "'".$ldap[$i]["memberOf"]."', ";
- $sqlquery .= "'".$ldap[$i]["manager"]."', ";
- $sqlquery .= "'".str_replace("'", $apos, $ldap[$i]["description"])."', ";
- $sqlquery .= "'".$ldap[$i]["personalTitle"]."', ";
- $sqlquery .= "'".$ldap[$i]["employeeID"]."', ";
- $sqlquery .= "'".$ldap[$i]["employeeType"]."', ";
- // ADS Eintrag (nie) abfangen
- if ($ldap[$i]["accountExpires"] != "" and $ldap[$i]["accountExpires"] != "0" and $ldap[$i]["accountExpires"] != "9223372036854775807")
- $sqlquery .= "'".date('Y-m-d H:i:s', $ldap[$i]["accountExpires"] / 10000000 - 11644473600)."', "; // ADS Epoch -> Unix Timestamp
- else
- $sqlquery .= "NULL, ";
- // ADS Eintrag (nie) abfangen
- if ($ldap[$i]["lastLogon"] != "" and $ldap[$i]["lastLogon"] != "0" and $ldap[$i]["lastLogon"] != "9223372036854775807")
- $sqlquery .= "'".date('Y-m-d H:i:s', $ldap[$i]["lastLogon"] / 10000000 - 11644473600)."', "; // ADS Epoch -> Unix Timestamp
- else
- $sqlquery .= "NULL, ";
- $sqlquery .= "'".$ldap[$i]["whenCreated"]."', ";
- $sqlquery .= "'".$ldap[$i]["objectGUID"]."'); ";
- // $sqlquery .= "'".date('Y-m-d H:i:s', $ldap[$i]["accountExpires"])."', ";
- // $sqlquery .= "'".date('Y-m-d H:i:s', $ldap[$i]["lastLogon"])."'); ";
- $sqlquery = utf8_encode($sqlquery);
- //echo $sqlquery."\n\n";
- if (!mysql_query($sqlquery, $mysql))
- {
- error_log("ERROR:".$sqlquery."".mysql_error()."<br>");
- $error = false;
- }
- }
- }
- return $error;
- }
- private function CreateMySQLGroupTable($ldap)
- {
- $mysql = $this->MySQLConnect();
- // $apos = "'";
- $apos = "\'";
- $error = true;
- mysql_query("TRUNCATE adsgroup;", $mysql); // Delete Data
- for($i=0; $i<count($ldap); $i++)
- {
- if($ldap[$i]["sid"] != "S-0-0")
- {
- $sqlquery = "INSERT INTO `adsgroup` (`sid`, `name`, `members`, `description`, `email`) VALUES";
- $sqlquery .= "('".$ldap[$i]["sid"]."', ";
- $sqlquery .= "'".str_replace("'", $apos, $ldap[$i]["name"])."', ";
- $sqlquery .= "'".str_replace("'", $apos, $ldap[$i]["member"])."', ";
- $sqlquery .= "'".$ldap[$i]["description"]."', ";
- $sqlquery .= "'".$ldap[$i]["mail"]."'); ";
- $sqlquery = utf8_encode($sqlquery);
- if (!mysql_query($sqlquery, $mysql))
- {
- error_log("ERROR:".$sqlquery."".mysql_error()."<br>");
- $error = false;
- }
- }
- }
- return $error;
- }
- public function ImportUsers()
- {
- $ldapquery = "(&(objectClass=Person)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(cn=*))";
- $search = array("sAMAccountName", "extensionAttribute6", "mail", "givenName", "sn", "physicalDeliveryOfficeName", "extensionAttribute5", "department", "departmentNumber", "description", "telephoneNumber", "mobile", "memberOf", "title", "personalTitle", "manager", "displayName", "sn", "employeeID", "employeeType", "lastLogon", "accountExpires", "facsimileTelephoneNumber");
- // Import Groups first
- $ldap_groups = $this->ImportGroups();
- // Query Attributes
- $ldap_result = $this->Ldap_Query($ldapquery, $search);
- if (count($ldap_result) < 100) die(); // LDAP-Error
- // get SIDs
- for($i=0; $i<count($ldap_result); $i++)
- {
- // Formatierung verschiedener Attribute
- $ldap_result[$i]["sid"] = $this->GetSid($ldap_result[$i]["sAMAccountName"]);
- $ldap_result[$i]["objectGUID"] = $this->GetObjectGUID($ldap_result[$i]["sAMAccountName"]);
- $ldap_result[$i]["whenCreated"] = $this->GetwhenCreated($ldap_result[$i]["sAMAccountName"]);
- // Get Groups recursive
- $group = explode (",", $ldap_result[$i]["memberOf"]);
- for($j=0; $j<count($group); $j++)
- {
- if (isset($ldap_groups[$group[$j]]))
- {
- $group[$j] = $ldap_groups[$group[$j]];
- }
- }
- $memberOf = "";
- for($j=0; $j<count($group); $j++)
- {
- $memberOf .= $group[$j].";";
- }
- //echo "[] ".$ldap_result[$i]["sAMAccountName"]." -> ". $ldap_result[$i]["memberOf"]."<br>";
- //echo "[][] ".$ldap_result[$i]["sAMAccountName"]." -> ". $memberOf."<br>";
- // add recursive groups to string
- //$memberOf = rtrim($memberOf, ",");
- $ldap_result[$i]["memberOf"] = $memberOf;
- }
- //var_dump($ldap_result);
- $error = $this->CreateMySQLUserTable($ldap_result);
- $this->UpdateSIDs();
- return $error;
- }
- public function getDeletedUsers()
- {
- $ldapquery = "(&(objectClass=Person)(userAccountControl:1.2.840.113556.1.4.803:=2)(cn=*))";
- $search = array("sAMAccountName");
- // Query Attributes
- $ldap_result = $this->Ldap_Query($ldapquery, $search);
- return $ldap_result;
- }
- public function ImportGroups()
- {
- $ldapquery = "(&(objectClass=Group)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(cn=*))";
- $search = array("sAMAccountName", "memberOf", "member", "mail", "description");
- // Query Attributes
- $ldap_result = $this->Ldap_Query($ldapquery, $search);
- for($i=0; $i<count($ldap_result); $i++)
- {
- if (!$ldap_result[$i]["memberOf"] == "")
- {
- // add recursive groups
- $group_result[$ldap_result[$i]["sAMAccountName"]] = $ldap_result[$i]["sAMAccountName"].",".$ldap_result[$i]["memberOf"];
- //echo $ldap_result[$i]["sAMAccountName"]." - ".$ldap_result[$i]["memberOf"]." - ".$ldap_result[$i]["member"]."<br>";
- }
- //echo $ldap_result[$i]["sAMAccountName"]." -> ".$this->GetSid($ldap_result[$i]["sAMAccountName"])."<br>";
- $group_map[$i]["name"] = $ldap_result[$i]["sAMAccountName"];
- $group_map[$i]["member"] = $ldap_result[$i]["member"];
- $group_map[$i]["mail"] = $ldap_result[$i]["mail"];
- $group_map[$i]["description"] = $ldap_result[$i]["description"];
- $group_map[$i]["sid"] = $this->GetSid($ldap_result[$i]["sAMAccountName"]);
- }
- $this->CreateMySQLGroupTable($group_map);
- return $group_result;
- }
- private function UpdateSIDs()
- {
- $mysql = $this->MySQLConnect();
- // create mapping array
- $sql = "SELECT displayname, sid FROM adsuser;";
- $result = mysql_query ($sql, $mysql);
- while ( $row = mysql_fetch_row ($result))
- {
- $sidmap[$row[0]] = $row[1];
- }
- $sql = "SELECT name, sid FROM adsgroup;";
- $result = mysql_query ($sql, $mysql);
- while ( $row = mysql_fetch_row ( $result ) )
- {
- $sidmap[$row[0]] = $row[1];
- }
- // Update SIDs in Grouptable
- $sql = "SELECT sid, members FROM adsgroup;";
- $i = 0;
- $sids="";
- $result = mysql_query ($sql, $mysql);
- while ( $row = mysql_fetch_row ( $result ) )
- {
- $names = explode(";", $row[1]);
- for ($j = 0; $j < count($names); $j++)
- {
- if(isset($sidmap[$names[$j]]))
- {
- $sids .= $sidmap[$names[$j]].";";
- }
- }
- $sids = rtrim($sids, ";");
- $sql1[$i] = "UPDATE adsgroup SET membersids='".$sids."' WHERE sid='".$row[0]."'";
- $i++;
- $sids = "";
- }
- for($j = 0; $j < count($sql1); $j++)
- {
- mysql_query ($sql1[$j], $mysql);
- //echo $sql1[$j]."<br><br>";
- }
- // Update SIDs in Usertable
- $sql = "SELECT sid, groups FROM adsuser;";
- $i = 0;
- $result = mysql_query ( $sql, $mysql);
- while ( $row = mysql_fetch_row ( $result ) )
- {
- $groups = explode(";", $row[1]);
- //var_dump($groups);
- //echo "<br>";
- for ($j = 0; $j < count($groups); $j++)
- {
- if(isset($sidmap[$groups[$j]]))
- {
- $sids .= $sidmap[$groups[$j]].";";
- }
- }
- $sids = rtrim($sids, ";");
- $sql2[$i] = "UPDATE adsuser SET groupsids='".$sids."' WHERE sid='".$row[0]."'";
- $i++;
- $sids="";
- }
- for($j = 0; $j < count($sql2); $j++)
- {
- mysql_query ($sql2[$j], $mysql);
- //echo $sql2[$j]."<br><br>";
- }
- }
- /*
- ****************************
- */
- public function SQLQuery($sql)
- {
- $mysql = $this->MySQLConnect();
- $result = mysql_query($sql, $mysql);
- if ($result == false) error_log("ERROR: " . $sql);
- if (!is_resource($result)) return $result;
- $i = 0;
- while ($row = mysql_fetch_array($result))
- {
- $array[$i] = $row;
- $i++;
- }
- if (!isset($array)) return "";
- return $array;
- }
- // sid or username
- public function GetGroups($sid)
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT groups, groupsids FROM adsuser WHERE username='".$sid."' OR sid='".$sid."';";
- $result = mysql_query ($sql, $mysql);
- while ( $row = mysql_fetch_row ( $result ) )
- {
- return $row[0].";".$row[1];
- }
- return "";
- }
- // alle Gruppen
- public function GetAllGroups()
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT DISTINCT groupname FROM groupmembership ORDER BY groupname;";
- $result = mysql_query ($sql, $mysql);
- $i = 0;
- while ( $row = mysql_fetch_row ( $result ) )
- {
- $groups[$i] = $row[0];
- $i++;
- }
- return $groups;
- }
- // Username or SID
- public function IsUserInGroup($sid, $groupsid)
- {
- $mysql = $this->MySQLConnect();
- // $sql = "SELECT * FROM groupmembership WHERE user_sid='".$sid."' AND group_sid = '".$groupsid."';";
- $sql = "SELECT * FROM groupmembership WHERE (user_sid='".$sid."' OR username='".$sid."') AND (group_sid = '".$groupsid."' OR groupname = '".$groupsid."');";
- $result = mysql_query ($sql, $mysql);
- while ( $row = mysql_fetch_array ( $result ) )
- {
- return true;
- }
- return false;
- /*
- $groups = $this->GetGroups($sid);
- $group = explode (";", $groups);
- for ($i=0; $i<count($group); $i++)
- {
- if ($group[$i] == $groupsid) return true;
- }
- return false;
- */
- }
- public function GetUsername($sid)
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT username FROM adsuser WHERE sid='".$sid."';";
- $result = mysql_query ($sql, $mysql);
- while ( $row = mysql_fetch_row ( $result ) )
- {
- return $row[0];
- }
- return "";
- }
- public function GetDisplayname($sid)
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT displayname FROM adsuser WHERE sid='".$sid."';";
- $result = mysql_query ($sql, $mysql);
- while ( $row = mysql_fetch_row ( $result ) )
- {
- return $row[0];
- }
- return "";
- }
- public function GetTelephone($sid)
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT telephone FROM adsuser WHERE sid='".$sid."';";
- $result = mysql_query ($sql, $mysql);
- while ( $row = mysql_fetch_row ( $result ) )
- {
- return $row[0];
- }
- return "";
- }
- public function GetUserData($sids)
- {
- $mysql = $this->MySQLConnect();
- foreach ($sids as $sid)
- {
- $sql = "SELECT username, display FROM adsuser WHERE sid='".$sid."';";
- $result = mysql_query ($sql, $mysql);
- while ( $row = mysql_fetch_row ( $result ) )
- {
- return $row[0];
- }
- }
- return "";
- }
- public function GetTransponder($sid)
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT transponder.id FROM transponder, adsuser WHERE adsuser.displayname = transponder.name AND (adsuser.username='".$sid."' OR adsuser.sid='".$sid."');";
- $result = mysql_query ($sql, $mysql);
- while ( $row = mysql_fetch_row ( $result ) )
- {
- return $row[0];
- }
- return "";
- }
- public function GetSIDfromGroups()
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT sid, name FROM adsgroup";
- $result = mysql_query($sql, $mysql);
- $groupmap = "";
- while ($row = mysql_fetch_row($result))
- {
- $groupmap[$row[1]] = $row[0];
- }
- return $groupmap;
- }
- // SID, Username or Displayname
- /* public function GetUserInformation($sid)
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT sid, username, email, displayname, description, title, forename, surname, room, department, departmentnumber, telephone, mobilephone, manager, groups, employeeid FROM adsuser ";
- $sql .= "WHERE username='".$sid."' OR sid='".$sid."' OR displayname='".$sid."';";
- $result = mysql_query($sql, $mysql);
- if ($result == false) error_log("ERROR: " . $sql);
- while ($row = mysql_fetch_row($result))
- {
- $userdata["sid"] = $row[0];
- $userdata["username"] = $row[1];
- $userdata["email"] = $row[2];
- $userdata["displayname"] = $row[3];
- $userdata["description"] = $row[4];
- $userdata["title"] = $row[5];
- $userdata["forename"] = $row[6];
- $userdata["surname"] = $row[7];
- $userdata["room"] = $row[8];
- $userdata["department"] = $row[9];
- $userdata["departmentnumber"] = $row[10];
- $userdata["telephone"] = $row[11];
- $userdata["mobilephone"] = $row[12];
- $userdata["manager"] = $row[13];
- $userdata["groups"] = $row[14];
- $userdata["employeeid"] = $row[15];
- return $userdata;
- }
- return false;
- }
- */
- public function GetUserInformation($sid)
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT sid, username, email, displayname, description, title, forename, surname, room, department, departmentnumber, telephone, mobilephone, manager,";
- $sql .= "groups, employeeid, position_de, position_en, proxy, proxy2, secretary, transponder, access, employeetype, institute, accountexpires, lastlogin FROM adsuser ";
- $sql .= "WHERE username='".$sid."' OR sid='".$sid."' OR displayname='".$sid."';";
- $result = mysql_query($sql, $mysql);
- if ($result == false) error_log("ERROR: " . $sql);
- while ($row = mysql_fetch_row($result))
- {
- $userdata["sid"] = $row[0];
- $userdata["username"] = $row[1];
- $userdata["email"] = $row[2];
- $userdata["displayname"] = $row[3];
- $userdata["description"] = $row[4];
- $userdata["title"] = $row[5];
- $userdata["forename"] = $row[6];
- $userdata["surname"] = $row[7];
- $userdata["room"] = $row[8];
- $userdata["department"] = $row[9];
- $userdata["departmentnumber"] = $row[10];
- $userdata["telephone"] = $row[11];
- $userdata["mobilephone"] = $row[12];
- $userdata["manager"] = $row[13];
- $userdata["groups"] = $row[14];
- $userdata["employeeid"] = $row[15];
- $userdata["position_de"] = $row[16];
- $userdata["position_en"] = $row[17];
- $userdata["proxy"] = $row[18];
- $userdata["proxy2"] = $row[19];
- $userdata["secretary"] = $row[20];
- $userdata["transponder"] = $row[21];
- $userdata["access"] = $row[22];
- $userdata["employeetype"] = $row[23];
- $userdata["institute"] = $row[24];
- $userdata["accountexpires"] = $row[25];
- $userdata["lastlogin"] = $row[26];
- return $userdata;
- }
- return false;
- }
- public function GetGroupInformation($sid)
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT sid, name, email, description FROM adsgroup ";
- $sql .= "WHERE name='".$sid."' OR sid='".$sid."';";
- $result = mysql_query($sql, $mysql);
- if ($result == false) error_log("ERROR: " . $sql);
- while ($row = mysql_fetch_row($result))
- {
- $userdata["sid"] = $row[0];
- $userdata["name"] = $row[1];
- $userdata["email"] = $row[2];
- $userdata["description"] = $row[3];
- return $userdata;
- }
- return false;
- }
- // sid or name
- public function GetUserInformationFromGroup($group)
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT sid, username, email, displayname, description, title, forename, surname, room, department, departmentnumber, telephone, mobilephone, manager, groups FROM adsuser ";
- $sql .= "WHERE groups LIKE '%".$group."%' OR groupsids LIKE '%".$group."%' ORDER BY displayname;";
- $result = mysql_query($sql, $mysql);
- if ($result == false) echo "ERROR: " . $sql;
- $userdata[0][0] = "";
- $i = 0;
- while ($row = mysql_fetch_row($result))
- {
- $userdata[$i]["sid"] = $row[0];
- $userdata[$i]["username"] = $row[1];
- $userdata[$i]["email"] = $row[2];
- $userdata[$i]["displayname"] = $row[3];
- $userdata[$i]["description"] = $row[4];
- $userdata[$i]["title"] = $row[5];
- $userdata[$i]["forename"] = $row[6];
- $userdata[$i]["surname"] = $row[7];
- $userdata[$i]["room"] = $row[8];
- $userdata[$i]["department"] = $row[9];
- $userdata[$i]["departmentnumber"] = $row[10];
- $userdata[$i]["telephone"] = $row[11];
- $userdata[$i]["mobilephone"] = $row[12];
- $userdata[$i]["manager"] = $row[13];
- $userdata[$i]["groups"] = $row[14];
- $i++;
- }
- return $userdata;
- }
- // sid or name
- private function GetMembersFromGroup($group)
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT membersids FROM adsgroup WHERE name = '".$group."' OR sid = '".$group."'";
- $result = mysql_query($sql, $mysql);
- $j = 0;
- while ( $row = mysql_fetch_row ( $result ) )
- {
- $members = $row[0];
- $j++;
- }
- if ($j > 0)
- {
- $user = explode(";", $members);
- for ($i = 0; $i<count($user); $i++)
- {
- $groups = $this->GetMembersFromGroup($user[$i]);
- if ($groups != null)
- {
- $user[$i] = $groups;
- }
- }
- return $user;
- }
- return null;
- }
- // sid or name
- public function GetUserSidsFromGroup($group)
- {
- $user = $this->GetMembersFromGroup($group);
- return explode(";",$this->MergeArray($user));
- }
- // sid
- public function GetUsersFromGroup($group)
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT DISTINCT group_sid, groupname, user_sid, displayname, username FROM groupmembership ";
- $sql .= "WHERE group_sid='".$group."' OR groupname='".$group."' ";
- $sql .= "ORDER BY displayname;";
- $result = mysql_query($sql, $mysql);
- $i = 0;
- $user[$i] = "";
- while ( $row = mysql_fetch_row ( $result ) )
- {
- if ($row[0] != "")
- {
- $user[$i]["sid"] = $row[2];
- $user[$i]["displayname"] = $row[3];
- $user[$i]["username"] = $row[4];
- $i++;
- }
- }
- return $user;
- }
- public function GetDepartmentNumbers()
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT DISTINCT departmentnumber, department FROM adsuser ORDER BY departmentnumber;";
- $result = mysql_query($sql, $mysql);
- $i = 0;
- $user[$i] = "";
- while ( $row = mysql_fetch_row ( $result ) )
- {
- if ($row[0] != "")
- {
- $user[$i]["nr"] = $row[0];
- $user[$i]["name"] = $row[1];
- $i++;
- }
- }
- return $user;
- }
- public function GetUsersFromDepartmentNumber($departmentNumber)
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT DISTINCT username, displayname, sid FROM adsuser WHERE departmentnumber = ".$departmentNumber.";";
- $result = mysql_query($sql, $mysql);
- $i = 0;
- $user[$i] = "";
- while ( $row = mysql_fetch_row ( $result ) )
- {
- $user[$i]["username"] = $row[0];
- $user[$i]["displayname"] = $row[1];
- $user[$i]["sid"] = $row[2];
- $i++;
- }
- return $user;
- }
- public function GetRooms()
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT * FROM telephone ORDER BY roomnr;";
- $result = mysql_query($sql, $mysql);
- $i = 0;
- $room[$i] = "";
- while ( $row = mysql_fetch_row ( $result ) )
- {
- $room[$i]["id"] = $row[0];
- $room[$i]["roomnr"] = $row[1];
- $room[$i]["description"] = $row[2];
- $room[$i]["telephonenr"] = $row[3];
- $i++;
- }
- return $room;
- }
- public function GetRoom($id)
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT * FROM telephone WHERE id = $id;";
- $result = mysql_query($sql, $mysql);
- $i = 0;
- $room = "";
- if ( $row = mysql_fetch_row ( $result ) )
- {
- $room["id"] = $row[0];
- $room["roomnr"] = $row[1];
- $room["description"] = $row[2];
- $room["telephonenr"] = $row[3];
- }
- return $room;
- }
- public function GetUserAccessFromDepartmentNumber($departmentNumber, $accessrights)
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT DISTINCT username, displayname, sid FROM adsuser WHERE departmentnumber = ".$departmentNumber." AND access = '".$accessrights."';";
- $result = mysql_query($sql, $mysql);
- $i = 0;
- $user[$i] = "";
- while ( $row = mysql_fetch_row ( $result ) )
- {
- $user[$i]["username"] = $row[0];
- $user[$i]["displayname"] = $row[1];
- $user[$i]["sid"] = $row[2];
- $i++;
- }
- return $user;
- }
- private function MergeArray($array)
- {
- $merged = "";
- for($i=0; $i<count($array); $i++)
- {
- if (!is_array($array[$i]))
- {
- $merged .= $array[$i].";";
- }
- else
- {
- $merged .= $this->MergeArray($array[$i]).";";
- }
- }
- $merged = str_replace(";;",";", $merged);
- $merged = trim($merged, ";");
- return $merged;
- }
- public function GetUserMap($group)
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT username, user_sid FROM groupmembership WHERE groupname = '".$group."';";
- //error_log($sql);
- $result = mysql_query($sql, $mysql);
- while ( $row = mysql_fetch_row ( $result ) )
- {
- if ($row[0] != "")
- {
- $user[$row[1]] = $row[0];
- }
- }
- return $user;
- }
- public function UpdateGroupMembership()
- {
- $apos = "\'";
- $mysql = $this->MySQLConnect();
- $groups = $this->GetSIDfromGroups();
- $sql = "SELECT sid, username, displayname FROM adsuser;";
- $result = mysql_query($sql, $mysql);
- while ( $row = mysql_fetch_row ( $result ) )
- {
- $usersid[$row[0]]["username"] = $row[1];
- $usersid[$row[0]]["displayname"] = str_replace("'", $apos, $row[2]);
- }
- $sql = "SELECT sid, name, description FROM adsgroup;";
- $result = mysql_query($sql, $mysql);
- while ( $row = mysql_fetch_row ( $result ) )
- {
- $groupsid[$row[0]]["name"] = $row[1];
- $groupsid[$row[0]]["description"] = $row[2];
- }
- $sql = "TRUNCATE groupmembership;";
- mysql_query($sql, $mysql);
- foreach ($groups as $group)
- {
- $users = $this->GetUserSidsFromGroup($group);
- foreach ($users as $user)
- {
- if (isset($usersid[$user]["username"]))
- {
- $sql = "INSERT INTO groupmembership (group_sid, groupname, user_sid, username, displayname, description) ";
- $sql .= "VALUES ('".$group."', '".$groupsid[$group]["name"]."', '".$user."', '".$usersid[$user]["username"]."', '".$usersid[$user]["displayname"]."', '".$groupsid[$group]["description"]."');";
- //if ($usersid[$user]["username"] == "mllu") echo $sql."\n";
- mysql_query($sql, $mysql);
- }
- }
- }
- }
- public function UpdatePosition()
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT position.sid, position.position_de, position.position_en, position.proxy, position.proxy2, position.access, position.manager FROM adsuser, position WHERE adsuser.sid = position.sid ";
- $result = mysql_query($sql, $mysql);
- while ( $row = mysql_fetch_row ( $result ) )
- {
- $proxy = $this->GetDisplayname($row[3]);
- $proxy2 = $this->GetDisplayname($row[4]);
- $manager = $this->GetDisplayname($row[6]);
- $sql = "UPDATE adsuser SET position_de = '".$row[1]."', position_en = '".$row[2]."', proxy = '".$proxy."', proxy2 = '".$proxy2."', access = '".$row[5]."', manager = '".$manager."' ";
- $sql .= "WHERE sid = '".$row[0]."';";
- //echo $sql."\n";
- mysql_query($sql, $mysql);
- }
- $sql = "SELECT DISTINCT adsuser.sid, adsuser.username, departments.secretary FROM departments, adsuser WHERE departments.oe = adsuser.departmentnumber AND departments.institute = adsuser.institute AND adsuser.departmentnumber != 'all';";
- $result = mysql_query($sql, $mysql);
- while ( $row = mysql_fetch_row ( $result ) )
- {
- $secretary = $this->GetDisplayname($row[2]);
- $sql = "UPDATE adsuser SET secretary = '".$secretary."' WHERE sid = '".$row[0]."';";
- mysql_query($sql, $mysql);
- }
- }
- public function UpdatePresence()
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT * FROM presence;";
- $i = 0;
- $result = mysql_query($sql, $mysql);
- while ( $row = mysql_fetch_array ( $result ) )
- {
- if ($row["sigma"] != NULL or $row["manual"] != NULL)
- $sql = "UPDATE adsuser SET present = 'N' ";
- else
- $sql = "UPDATE adsuser SET present = 'Y' ";
- $sql .= "WHERE username = '".$row["username"]."';";
- //echo $sql."\n";
- mysql_query($sql, $mysql);
- }
- }
- public function UpdateTransponder()
- {
- $mysql = $this->MySQLConnect();
- $sql = "SELECT transponder.id, adsuser.sid FROM transponder, adsuser WHERE adsuser.employeeid = transponder.employeeid AND adsuser.employeeid != ''";
- $result = mysql_query($sql, $mysql);
- while ( $row = mysql_fetch_row ( $result ) )
- {
- $sql = "UPDATE adsuser SET transponder = '".$row[0]."' ";
- $sql .= "WHERE sid = '".$row[1]."';";
- //echo $sql."\n";
- mysql_query($sql, $mysql);
- }
- }
- public function Query($query)
- {
- $mysql = $this->MySQLConnect();
- if (isset($_GET["debug"])) echo $query;
- //error_log($query);
- $result = mysql_query($query,$mysql);
- $i = 0;
- while ($row = mysql_fetch_array($result))
- {
- $array[$i] = $row;
- $i++;
- }
- if (!isset($array)) return "";
- return $array;
- }
- }
- ?>
|