Advertisement
chasgrundy

PHP LDAP simple JSON web service

Jan 19th, 2014
739
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. $q = strtolower(preg_replace('/[^0-9a-zA-Z@\.]/i','',$_GET['q']));
  3. $attribute = strtolower(preg_replace('/[^0-9a-zA-Z]/i','',$_GET['attribute']));
  4.  
  5. if (empty($q) || empty($attribute)){ echo "Missing parameter. Format should be index.php?q=XXXXXXX&attribute=YYYYYYY.\n\nq may be a NetID or email address.\nattribute may be any valid LDAP attribute (e.g., ndMail, telephoneNumber, or ndPrimaryAffiliation).\n\nAll values are case insensitive.\n\nSee eds.nd.edu for possible attributes and values."; die;}
  6.  
  7. $ds=ldap_connect("ldaps://ldap.nd.edu",636);  // must be a valid LDAP server!
  8.  
  9. if ($ds) {
  10.     $r=ldap_bind($ds);     // this is an "anonymous" bind, typically
  11.                            // read-only access
  12.  
  13.     if (strpos($q, '@')) {
  14.         $filter = "ndmail=".$q;
  15.     }
  16.     else {
  17.         $filter = "uid=".$q;
  18.     }
  19.  
  20.     $sr=ldap_search($ds, "o=University of Notre Dame,st=Indiana,c=US", $filter);  
  21.  
  22.     $info = ldap_get_entries($ds, $sr);
  23.  
  24.     echo "{\n   \"title\": \"LDAP\",\n      \"attributes\": {\n";
  25.  
  26.     for ($i=0; $i<$info["count"]; $i++) {
  27.         echo "         \"netid\": \"" . $info[$i]["uid"][0] . "\",\n";
  28.         echo "         \"".$attribute."\": \"" . $info[$i][$attribute][0] . "\"\n";
  29.     }
  30.  
  31.     ldap_close($ds);
  32.     echo "      }\n}";
  33.  
  34. } else {
  35.     echo "{ \"Unable to connect to LDAP server\" }";
  36. }
  37. ?>
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement