it actually works, fuck me
[ga4php.git] / authserver / lib / authClient.php
1 <?php
2
3 require_once("lib.php");
4
5 class GAAuthClient {
6         
7         // this functiuon will now act as our generic send/recieve client funciton
8         // im doing this because im going to move from ipc messaging to a tcp connection
9         // shortly and i want to encapsulate the send/receive behaviour
10         // things we need to add here are:
11         // 1) a way of saying "more data coming" cause getusers wont fit into one message
12         // 2) timeouts and locking
13         
14         // io think this function should now "work" more or less as is
15         function sendReceive($message_type, $message) {
16                 // yeah... this is totally gunna work
17                 global $TCP_PORT_NUMBER;
18                 
19                 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
20                 $res = socket_connect($socket, "127.0.0.1", $TCP_PORT_NUMBER);
21                 if(!$res) {
22                         socket_close($socket);
23                         return false;
24                 }
25                 
26                 $msg["type"] = $message_type;
27                 $msg["data"] = $message;
28                 
29                 $datacomp = base64_encode(serialize($msg));
30                 $tosend = "AC:$datacomp:EOD";
31                 
32                 socket_send($socket, $tosend, strlen($tosend), 0);
33                 
34                 // get up to one meg of data - this is bad... i can feel this function
35                 // hurting alot
36                 // TODO FIX THIS - its garbage code... im not really sure how to handle this really
37                 // we need to read back as AS:data:EOD - i think it now does.. i hope, tho we need
38                 // timeouts now.
39                 $recvd = "";
40                 $continue = true;
41                 while($continue) {
42                         $size = socket_recv($socket, $recvd_a, 1024, 0);
43                         $recvd .= $recvd_a;
44                         if(preg_match("/.*\:EOD$/", $recvd)) {
45                                 // we have a full string... break out
46                                 $continue = false;
47                                 break;
48                         }
49                 }
50                 
51                 
52                 // first check we got something that makes sense
53                 if(preg_match("/^AS:.*:EOD/", $recvd) < 1) {
54                         socket_close($socket);
55                         // we have a problem jim
56                         return false;
57                 }
58                 
59                 $xps = explode(":", $recvd);
60                 
61                 $component =  unserialize(base64_decode($xps[1]));
62                 
63                 if($component["type"] != $message_type) {
64                         // we have a problem jim
65                         socket_close($socket);
66                         return false;
67                 }
68                 
69                 socket_close($socket);
70                 
71                 return $component["data"];
72         }
73         
74         function addRadiusClient($clientname, $clientip, $clientsecret, $clientdesc) {
75         
76                 $message["clientname"] = $clientname;
77                 $message["clientsecret"] = $clientsecret;
78                 $message["clientip"] = $clientip;
79                 $message["clientdescription"] = $clientdesc;
80                 
81                 return $this->sendReceive(MSG_ADD_RADIUS_CLIENT, $message);
82         }
83
84         function deleteRadiusClient($clientname) {
85                 $message["clientname"] = $clientname;
86                 
87                 return $this->sendReceive(MSG_REMOVE_RADIUS_CLIENT, $message);
88                 
89         }
90         
91         function getRadiusClients() {
92                 return $this->sendReceive(MSG_GET_RADIUS_CLIENTS, "");
93         }
94         
95         
96         function syncUserToken($username, $tokenone, $tokentwo) {
97                 $message["username"] = $username;
98                 $message["tokenone"] = $tokenone;
99                 $message["tokentwo"] = $tokentwo;
100
101                 return $this->sendReceive(MSG_SYNC_TOKEN, $messgae);
102         }
103         
104         function getUserTokenType($username) {
105                 $message["username"] = $username;
106
107                 return $this->sendReceive(MSG_GET_TOKEN_TYPE, $message);                
108         }
109         
110         function setUserToken($username, $token) {
111                 $message["username"] = $username;
112                 $message["tokenstring"] = $token;
113                 
114                 return $this->sendReceive(MSG_GET_USER_TOKEN, $message);                
115         }
116         
117         function setUserPass($username, $password) {
118                 $message["username"] = $username;
119                 $message["password"] = $password;
120                 
121                 return $this->sendReceive(MSG_SET_USER_PASSWORD, $message);
122         }
123         
124         function getOtkID($username) {
125                 $message["username"] = $username;
126
127                 return $this->sendReceive(MSG_GET_OTK_ID, $message);
128         }
129         
130         function getOtkPng($username, $otk) {
131                 $message["otk"] = $otk;
132                 $message["username"] = $username;
133
134                 return $this->sendReceive(MSG_GET_OTK_PNG, $message);
135         }
136         
137         function authUserPass($username, $password) {
138                 $message["username"] = $username;
139                 $message["password"] = $password;
140                 
141                 return $this->sendReceive(MSG_AUTH_USER_PASSWORD, $message);            
142         }
143         
144         function deleteUser($username) {
145                 $message["username"] = $username;
146                 
147                 return $this->sendReceive(MSG_DELETE_USER, $message);
148         }
149         
150         function setUserRealName($username, $realname) {
151                 $message["username"] = $username;
152                 $message["realname"] = $realname;
153                 
154                 return $this->sendReceive(MSG_SET_USER_REALNAME, $message);             
155         }
156         
157         function getUsers() {
158                 return $this->sendReceive(MSG_GET_USERS, "");
159         }
160         
161         function authUserToken($username, $passcode) {
162                 $message["username"] = $username;
163                 $message["passcode"] = $passcode;
164                 
165                 return $this->sendReceive(MSG_AUTH_USER_TOKEN, $message);
166         }
167         
168         function deleteUserToken($username) {
169                 $message["username"] = $username;
170                 
171                 return $this->sendReceive(MSG_DELETE_USER_TOKEN, $message);
172         }
173         
174         function addUser($username, $tokentype="", $hexkey="") {
175                 $message["username"] = $username;
176                 if($tokentype!="") $message["tokentype"] = $tokentype;
177                 if($hexkey!="") $message["hexkey"] = $hexkey;
178                 
179                 return $this->sendReceive(MSG_ADD_USER_TOKEN, $message);
180         }
181
182         function setUserTokenType($username, $tokentype) {
183                 $message["username"] = $username;
184                 $message["tokentype"] = $tokentype;
185                 
186                 return $this->sendReceive(MSG_SET_USER_TOKEN_TYPE, $message);
187         }
188 }
189
190 ?>