Working AD basic html page.
[ga4php.git] / example / activedirectory / extend.php
1 <?php
2
3 require_once("../../lib/ga4php.php");
4
5 // TODO: This code works, but needs to be fixed and commented properly
6
7
8 // define our token class
9 class myGA extends GoogleAuthenticator {
10         function getData($username) {
11                 global $dsconnect, $host, $binduser, $bindpass, $basecn;
12                 
13                 // set this to default to begin with
14                 $tokendata = false;
15                 
16                 // we search for a username that matches what we've been passed
17                 $sr = ldap_search($dsconnect, "$basecn", "samaccountname=$username");
18                 $info = ldap_get_entries($dsconnect, $sr);
19                 
20                 //echo "<pre>";
21                 //print_r($info);
22                 //echo "</pre>";
23                 
24                 $attr_name = false;
25                 for($i=1; $i<15; $i++) {
26                         $valname = "extensionattribute$i";
27                         if(isset($info[0]["$valname"][0])) {
28                                 $val = $info[0]["$valname"][0];
29                                 // we are looking for an extension attribute that has a start of "ga4php"
30                                 if(preg_match('/^ga4php.*/', $val)>0) {
31                                         $attr_name = $valname;
32                                 }
33                         }
34                         
35                 }
36                 
37                 // yeah, totally works.... HAH
38                 if($attr_name != false) {
39                         $tokend = $info[0]["$attr_name"][0];
40                         $expl = explode(":", $tokend);
41                         $tokendata = $expl[1];
42                 }
43                                 
44                 return $tokendata;
45                 
46                 // and there you have it, simple eh?
47         }
48         
49         
50         // now we need a function for putting the data back into our user table.
51         // in this example, we wont check anything, we'll just overwrite it.
52         function putData($username, $data) {
53                 global $dsconnect, $host, $binduser, $bindpass, $basecn;
54                 
55                 if($data!="") {
56                         $data .= "ga4php:";
57                 }
58                 
59                 // set this to default to begin with
60                 $tokendata = false;
61                 
62                 // we need to track the "first" blank attribute
63                 $blank_attr = false;
64                 
65                 // we search for a username that matches what we've been passed
66                 $sr = ldap_search($dsconnect, "$basecn", "samaccountname=$username");
67                 $info = ldap_get_entries($dsconnect, $sr);
68                 $dn = $info[0]["distinguishedname"][0];
69                 
70                 //echo "<pre>";
71                 //print_r($info);
72                 //echo "</pre>";
73                 
74                 $attr_name = false;
75                 for($i=1; $i<15; $i++) {
76                         $valname = "extensionattribute$i";
77                         if(isset($info[0]["$valname"][0])) {
78                                 $val = $info[0]["$valname"][0];
79                                 // we are looking for an extension attribute that has a start of "ga4php"
80                                 if(preg_match('/^ga4php.*/', $val)>0) {
81                                         $attr_name = $valname;
82                                 }
83                         } else {
84                                 if($blank_attr == false) {
85                                         // this will cathc the first unset extension variable name, if we need it
86                                         $blank_attr = "$valname";
87                                 }
88                         }
89                         
90                 }
91                 
92                 // if the attr_name is not set, we need to set $blank_attr
93                 if($attr_name == false) {
94                         // we use $blank_attr
95                         error_log("setting for $username, $blank_attr");
96                         $infod["$blank_attr"][0] = "$data";
97                 } else {
98                         error_log("setting for $username, $attr_name");
99                         $infod["$attr_name"][0] = "$data";
100                 }
101                 
102                 error_log("att end of put data for $dn, $infod");
103                 
104                 return ldap_modify($dsconnect, $dn, $infod); 
105                 // even simpler!
106         }
107         
108         // not implemented yet
109         function getUsers() {
110                 return false;
111         }
112 }
113
114 ?>