Working AD basic html page.
[ga4php.git] / example / activedirectory / index.php
1 <?php 
2 /*
3  * This example shows how you might store user data directly into AD.
4  * AD has several attributes you can use for storing your own data, and
5  * thats what we use
6  * 
7  * This is only the beginning code, 
8  */
9
10 // set these
11 $host = ""; // for eg "1.2.3.4"
12 $binduser = ""; // for eg "administrator"
13 $bindpass = ""; // for eg "password"
14 $basecn = ""; // for eg "CN=users, DC=google, dc=com"
15
16 //require our GoogleAuthenticator sub classed class
17 require_once("extend.php");
18 $myga = new myGA();
19
20 // this is here so i can keep my atributes somewhere in the tree and not have them float around on git/svn
21 if(file_exists("../../../../.dontappearingitandsvn.php")) require_once("../../../../.dontappearingitandsvn.php");
22
23 $error = false;
24
25 // first, lets bind our AD with out management creds
26 error_log("host is $host");
27 $dsconnect = ldap_connect("$host", 389);
28
29 // we mark it global so we can get it in our class
30 global $dsconnect, $host, $binduser, $bindpass, $basecn;
31
32 if(!$dsconnect) {
33         $error = true;
34         $errorText = "Can't Connect to AD";
35 }
36 $ldapbind = ldap_bind($dsconnect, "$binduser", "$bindpass");
37 ?>
38 <html>
39 <H1>Welcome to GA4PHP Talking to Active Directory</H1>
40
41 <?php
42 if($error) {
43         echo "<font color=\"red\">$errorText</font><br>";
44 }
45 ?>
46
47 Our user list within AD:
48 <table border="1">
49 <tr><th>Name</th><th>Login Name</th></tr>
50 <?php 
51         $sr = ldap_search($dsconnect, "$basecn", "objectclass=user");
52         $info = ldap_get_entries($dsconnect, $sr);
53         //$info["extensionattribute2"] = "-----";
54         
55         
56         //print_r($info);
57         $i = 0;
58         foreach($info as $key => $val) {
59                 //echo "$key is ".$val["distinguishedname"][0]."\n";
60                 if($val["distinguishedname"][0] != "") {
61                         $user[$i]["dn"] = $val["distinguishedname"][0];
62                         $user[$i]["acn"] = $val["samaccountname"][0];
63                         $user[$i]["cn"] = $val["cn"][0];
64                 }
65
66                 $i ++;
67                 //return 0;
68         }
69         
70         foreach($user as $value) {
71                 $cn = $value["cn"];
72                 $un = $value["acn"];
73                 echo "<tr><td>$cn</td><td>$un</td></tr>";
74         }
75 ?>
76
77
78
79 </table>
80 testing administrator<br>
81 <?php
82 if($myga->hasToken("administrator")) {
83         echo "administrator has a token<br>";
84 } else {
85         echo "administrator has no token, setting one<br>";
86         $myga->setUser("administrator");
87 }
88 ?>
89 </html>