3 require_once("lib.php");
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
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;
19 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
20 $res = socket_connect($socket, "127.0.0.1", $TCP_PORT_NUMBER);
22 socket_close($socket);
26 $msg["type"] = $message_type;
27 $msg["data"] = $message;
29 $datacomp = base64_encode(serialize($msg));
30 $tosend = "AC:$datacomp:EOD";
32 socket_send($socket, $tosend, strlen($tosend), 0);
34 // get up to one meg of data - this is bad... i can feel this function
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
42 $size = socket_recv($socket, $recvd_a, 1024, 0);
44 if(preg_match("/.*\:EOD$/", $recvd)) {
45 // we have a full string... break out
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
59 $xps = explode(":", $recvd);
61 $component = unserialize(base64_decode($xps[1]));
63 if($component["type"] != $message_type) {
64 // we have a problem jim
65 socket_close($socket);
69 socket_close($socket);
71 return $component["data"];
74 function sendReceiveIPC($message_type, $message) {
75 global $MSG_QUEUE_KEY_ID_SERVER, $MSG_QUEUE_KEY_ID_CLIENT;
78 if(!msg_queue_exists($MSG_QUEUE_KEY_ID_SERVER)) {
82 if(!msg_queue_exists($MSG_QUEUE_KEY_ID_CLIENT)) {
85 // TODO we need to setup a client queue sem lock here
87 $cl_queue = msg_get_queue($MSG_QUEUE_KEY_ID_CLIENT);
88 $sr_queue = msg_get_queue($MSG_QUEUE_KEY_ID_SERVER);
90 msg_send($sr_queue, $message_type, $message, true, true, $msg_err);
91 msg_receive($cl_queue, 0, $msg_type, 131072, $msg);
96 function addRadiusClient($clientname, $clientip, $clientsecret, $clientdesc) {
98 $message["clientname"] = $clientname;
99 $message["clientsecret"] = $clientsecret;
100 $message["clientip"] = $clientip;
101 $message["clientdescription"] = $clientdesc;
103 return $this->sendReceive(MSG_ADD_RADIUS_CLIENT, $message);
106 function deleteRadiusClient($clientname) {
107 $message["clientname"] = $clientname;
109 return $this->sendReceive(MSG_REMOVE_RADIUS_CLIENT, $message);
113 function getRadiusClients() {
114 return $this->sendReceive(MSG_GET_RADIUS_CLIENTS, "");
118 function syncUserToken($username, $tokenone, $tokentwo) {
119 $message["username"] = $username;
120 $message["tokenone"] = $tokenone;
121 $message["tokentwo"] = $tokentwo;
123 return $this->sendReceive(MSG_SYNC_TOKEN, $messgae);
126 function getUserTokenType($username) {
127 $message["username"] = $username;
129 return $this->sendReceive(MSG_GET_TOKEN_TYPE, $message);
132 function setUserToken($username, $token) {
133 $message["username"] = $username;
134 $message["tokenstring"] = $token;
136 return $this->sendReceive(MSG_GET_USER_TOKEN, $message);
139 function setUserPass($username, $password) {
140 $message["username"] = $username;
141 $message["password"] = $password;
143 return $this->sendReceive(MSG_SET_USER_PASSWORD, $message);
146 function getOtkID($username) {
147 $message["username"] = $username;
149 return $this->sendReceive(MSG_GET_OTK_ID, $message);
152 function getOtkPng($username, $otk) {
153 $message["otk"] = $otk;
154 $message["username"] = $username;
156 return $this->sendReceive(MSG_GET_OTK_PNG, $message);
159 function authUserPass($username, $password) {
160 $message["username"] = $username;
161 $message["password"] = $password;
163 return $this->sendReceive(MSG_AUTH_USER_PASSWORD, $message);
166 function deleteUser($username) {
167 $message["username"] = $username;
169 return $this->sendReceive(MSG_DELETE_USER, $message);
172 function setUserRealName($username, $realname) {
173 $message["username"] = $username;
174 $message["realname"] = $realname;
176 return $this->sendReceive(MSG_SET_USER_REALNAME, $message);
179 function getUsers() {
180 return $this->sendReceive(MSG_GET_USERS, "");
183 function authUserToken($username, $passcode) {
184 $message["username"] = $username;
185 $message["passcode"] = $passcode;
187 return $this->sendReceive(MSG_AUTH_USER_TOKEN, $message);
190 function deleteUserToken($username) {
191 $message["username"] = $username;
193 return $this->sendReceive(MSG_DELETE_USER_TOKEN, $message);
196 function addUser($username, $tokentype="", $hexkey="") {
197 $message["username"] = $username;
198 if($tokentype!="") $message["tokentype"] = $tokentype;
199 if($hexkey!="") $message["hexkey"] = $hexkey;
201 return $this->sendReceive(MSG_ADD_USER_TOKEN, $message);
204 function setUserTokenType($username, $tokentype) {
205 $message["username"] = $username;
206 $message["tokentype"] = $tokentype;
208 return $this->sendReceive(MSG_SET_USER_TOKEN_TYPE, $message);