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