3 if(file_exists("config.php")) {
4 require_once("config.php");
6 // config file doesnt exist, we must abort sensibly
9 // get out master library for ga4php
10 require_once("../lib/lib.php");
14 // first we want to fork into the background like all good daemons should
15 //$pid = pcntl_fork();
21 // i am the parent, i shall leave
22 echo "i am a parent, i leave\n";
25 global $MSG_QUEUE_KEY_ID_SERVER, $MSG_QUEUE_KEY_ID_CLIENT;
27 $cl_queue = msg_get_queue($MSG_QUEUE_KEY_ID_CLIENT, 0666 | 'IPC_CREAT');
28 $sr_queue = msg_get_queue($MSG_QUEUE_KEY_ID_SERVER, 0666 | 'IPC_CREAT');
37 msg_receive($sr_queue, 0, $msg_type, 16384, $msg);
40 case MSG_AUTH_USER_TOKEN:
41 echo "Call to auth user token\n";
42 // minimal checking, we leav it up to authenticateUser to do the real
44 if(!isset($msg["username"])) $msg["username"] = "";
45 if(!isset($msg["passcode"])) $msg["passcode"] = "";
46 $username = $msg["username"];
47 $passcode = $msg["passcode"];
49 $authval = $myga->authenticateUser($username, $passcode);
50 msg_send($cl_queue, MSG_AUTH_USER_TOKEN, $authval);
52 case MSG_ADD_USER_TOKEN:
53 echo "Call to add user token\n";
54 if(!isset($msg["username"])) {
55 msg_send($cl_queue, MSG_ADD_USER_TOKEN, false);
57 $username = $msg["username"];
59 if(isset($msg["tokentype"])) {
63 if(isset($msg["hexkey"])) {
64 $hexkey = $msg["hexkey"];
67 $myga->setUser($username, $tokentype, "", $hexkey);
69 msg_send($cl_queue, MSG_ADD_USER_TOKEN, $myga->createUrl($username));
73 echo "Call to del user\n";
74 if(!isset($msg["username"])) {
75 msg_send($cl_queue, MSG_DELETE_USER, false);
77 $username = $msg["username"];
79 $sql = "delete from users where users_username='$username'";
83 msg_send($cl_queue, MSG_DELETE_USER, true);
86 case MSG_AUTH_USER_PASSWORD:
88 echo "Call to auth user pass\n";
89 if(!isset($msg["username"])) {
90 msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
93 if(!isset($msg["password"])) {
94 msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
98 $username = $msg["username"];
99 $password = $msg["password"];
100 $sql = "select users_password from users where users_username='$username'";
101 $dbo = getDatabase();
102 $res = $dbo->query($sql);
104 foreach($res as $row) {
105 $pass = $row["users_password"];
109 $ourpass = hash('sha512', $password);
110 echo "ourpass: $ourpass\nourhash: $pass\n";
111 if($ourpass == $pass) {
112 msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, true);
115 msg_send($cl_queue, MSG_AUTH_USER_PASSWORD, false);
120 case MSG_SET_USER_PASSWORD:
121 echo "Call to set user pass\n";
123 if(!isset($msg["username"])) {
124 msg_send($cl_queue, MSG_SET_USER_PASSWORD, false);
127 if(!isset($msg["password"])) {
128 msg_send($cl_queue, MSG_SET_USER_PASSWORD, false);
132 $username = $msg["username"];
133 $password = $msg["password"];
135 $pass = hash('sha512', $password);
137 $dbo = getDatabase();
138 $sql = "update users set users_password='$pass' where users_username='$username'";
142 msg_send($cl_queue, MSG_SET_USER_REALNAME, true);
145 // these are irrelavent yet
148 case MSG_SET_USER_REALNAME:
149 echo "Call to set user realname\n";
151 if(!isset($msg["username"])) {
152 msg_send($cl_queue, MSG_SET_USER_REALNAME, false);
155 if(!isset($msg["realname"])) {
156 msg_send($cl_queue, MSG_SET_USER_REALNAME, false);
160 $username = $msg["username"];
161 $realname = $msg["realname"];
162 $sql = "update users set users_realname='$realname' where users_username='$username'";
163 $dbo = getDatabase();
167 msg_send($cl_queue, MSG_SET_USER_REALNAME, true);
169 // TODO now set real name
171 case MSG_SET_USER_TOKEN:
173 echo "Call to set user token\n";
174 if(!isset($msg["username"])) {
175 msg_send($cl_queue, MSG_SET_USER_TOKEN, false);
178 if(!isset($msg["tokenstring"])) {
179 msg_send($cl_queue, MSG_SET_USER_TOKEN, false);
184 $username = $msg["username"];
185 $token = $msg["tokenstring"];
186 $return = $myga->setUserKey($username, $token);
187 msg_send($cl_queue, MSG_SET_USER_TOKEN, $return);
189 // TODO now set token
191 case MSG_SET_USER_TOKEN_TYPE:
193 echo "Call to set user token type\n";
194 if(!isset($msg["username"])) {
195 msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, false);
198 if(!isset($msg["tokentype"])) {
199 msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, false);
203 $username = $msg["username"];
204 $tokentype = $msg["tokentype"];
206 msg_send($cl_queue, MSG_SET_USER_TOKEN_TYPE, $myga->setTokenType($username, $tokentype));
208 // TODO now set token
211 // TODO this needs to be better
212 $sql = "select * from users";
214 $dbo = getDatabase();
215 $res = $dbo->query($sql);
219 foreach($res as $row) {
220 $users[$i]["username"] = $row["users_username"];
221 $users[$i]["realname"] = $row["users_realname"];
222 if($row["users_password"]!="") {
223 $users[$i]["haspass"] = true;
225 $users[$i]["haspass"] = false;
227 echo "user: ".$users[$i]["username"]." has tdata: \"".$row["users_tokendata"]."\"\n";
228 if($row["users_tokendata"]!="") {
229 $users[$i]["hastoken"] = true;
231 $users[$i]["hastoken"] = false;
235 msg_send($cl_queue, MSG_GET_USERS, $users);
237 // TODO now set token