added the tcp code in, but its not running yet
[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 sendReceiveTcp($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));
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 sendReceive($message_type, $message) {
75                 global $MSG_QUEUE_KEY_ID_SERVER, $MSG_QUEUE_KEY_ID_CLIENT;
76                 
77                 
78                 if(!msg_queue_exists($MSG_QUEUE_KEY_ID_SERVER)) {
79                         return false;
80                 }
81
82                 if(!msg_queue_exists($MSG_QUEUE_KEY_ID_CLIENT)) {
83                         return false;
84                 }
85                 // TODO we need to setup a client queue sem lock here
86                 
87                 $cl_queue = msg_get_queue($MSG_QUEUE_KEY_ID_CLIENT);
88                 $sr_queue = msg_get_queue($MSG_QUEUE_KEY_ID_SERVER);
89                 
90                 msg_send($sr_queue, $message_type, $message, true, true, $msg_err);
91                 msg_receive($cl_queue, 0, $msg_type, 131072, $msg);
92                 
93                 return $msg;
94         }
95         
96         function addRadiusClient($clientname, $clientip, $clientsecret, $clientdesc) {
97         
98                 $message["clientname"] = $clientname;
99                 $message["clientsecret"] = $clientsecret;
100                 $message["clientip"] = $clientip;
101                 $message["clientdescription"] = $clientdesc;
102                 
103                 return $this->sendReceive(MSG_ADD_RADIUS_CLIENT, $message);
104         }
105
106         function deleteRadiusClient($clientname) {
107                 $message["clientname"] = $clientname;
108                 
109                 return $this->sendReceive(MSG_REMOVE_RADIUS_CLIENT, $message);
110                 
111         }
112         
113         function getRadiusClients() {
114                 return $this->sendReceive(MSG_GET_RADIUS_CLIENTS, "");
115         }
116         
117         
118         function syncUserToken($username, $tokenone, $tokentwo) {
119                 $message["username"] = $username;
120                 $message["tokenone"] = $tokenone;
121                 $message["tokentwo"] = $tokentwo;
122
123                 return $this->sendReceive(MSG_SYNC_TOKEN, $messgae);
124         }
125         
126         function getUserTokenType($username) {
127                 $message["username"] = $username;
128
129                 return $this->sendReceive(MSG_GET_TOKEN_TYPE, $message);                
130         }
131         
132         function setUserToken($username, $token) {
133                 $message["username"] = $username;
134                 $message["tokenstring"] = $token;
135                 
136                 return $this->sendReceive(MSG_GET_USER_TOKEN, $message);                
137         }
138         
139         function setUserPass($username, $password) {
140                 $message["username"] = $username;
141                 $message["password"] = $password;
142                 
143                 return $this->sendReceive(MSG_SET_USER_PASSWORD, $message);
144         }
145         
146         function getOtkID($username) {
147                 $message["username"] = $username;
148
149                 return $this->sendReceive(MSG_GET_OTK_ID, $message);
150         }
151         
152         function getOtkPng($username, $otk) {
153                 $message["otk"] = $otk;
154                 $message["username"] = $username;
155
156                 return $this->sendReceive(MSG_GET_OTK_PNG, $message);
157         }
158         
159         function authUserPass($username, $password) {
160                 $message["username"] = $username;
161                 $message["password"] = $password;
162                 
163                 return $this->sendReceive(MSG_AUTH_USER_PASSWORD, $message);            
164         }
165         
166         function deleteUser($username) {
167                 $message["username"] = $username;
168                 
169                 return $this->sendReceive(MSG_DELETE_USER, $message);
170         }
171         
172         function setUserRealName($username, $realname) {
173                 $message["username"] = $username;
174                 $message["realname"] = $realname;
175                 
176                 return $this->sendReceive(MSG_SET_USER_REALNAME, $message);             
177         }
178         
179         function getUsers() {
180                 return $this->sendReceive(MSG_GET_USERS, "");
181         }
182         
183         function authUserToken($username, $passcode) {
184                 $message["username"] = $username;
185                 $message["passcode"] = $passcode;
186                 
187                 return $this->sendReceive(MSG_AUTH_USER_TOKEN, $message);
188         }
189         
190         function deleteUserToken($username) {
191                 $message["username"] = $username;
192                 
193                 return $this->sendReceive(MSG_DELETE_USER_TOKEN, $message);
194         }
195         
196         function addUser($username, $tokentype="", $hexkey="") {
197                 $message["username"] = $username;
198                 if($tokentype!="") $message["tokentype"] = $tokentype;
199                 if($hexkey!="") $message["hexkey"] = $hexkey;
200                 
201                 return $this->sendReceive(MSG_ADD_USER_TOKEN, $message);
202         }
203
204         function setUserTokenType($username, $tokentype) {
205                 $message["username"] = $username;
206                 $message["tokentype"] = $tokentype;
207                 
208                 return $this->sendReceive(MSG_SET_USER_TOKEN_TYPE, $message);
209         }
210 }
211
212 ?>