Merge branch '138-toggle-free-look-with-hotkey' into 'main/atys-live'
[ryzomcore.git] / web / public_php / admin / nel / nel_message.php
blobab0e55f0e9d54b61e359227d9a3b9897bdb60172
1 <?php
3 $SockTimeOut = 10;
5 function debug($text)
7 // flush();
8 // echo $text;
10 // $file = "logs/debug.txt";
11 // $str = file_get_contents($file);
12 // $str .= $text."\n";
13 // file_put_contents($file, $str);
16 class CMemStream
18 var $Buffer;
19 var $InputStream;
20 var $Pos;
22 function __construct()
24 $this->InputStream = false;
25 $this->Pos = 0;
26 $this->Buffer = "";
27 debug("A : ".gettype($this->Buffer)."<br>");
30 function setBuffer ($Buffer)
32 $this->InputStream = true;
33 $this->Buffer = $Buffer;
34 $this->Pos = 0;
37 function isReading () { return $this->InputStream; }
39 function serialUInt8 (&$val)
41 if ($this->isReading())
43 $val = ord($this->Buffer{$this->Pos++});
44 debug(sprintf ("read uint8 '%d'<br>\n", $val));
46 else
48 debug("B".gettype($this->Buffer)."<br>");
49 debug(sprintf ("write uint8 Buffer size before = %u<br>\n", strlen($this->Buffer)));
50 $this->Buffer = $this->Buffer . chr($val & 0xFF);
51 $this->Pos++;
52 debug("C".gettype($this->Buffer)."<br>");
53 debug(sprintf ("write uint8 '%d' %d<br>\n", $val, $this->Pos));
54 debug(sprintf ("write uint8 Buffer size after = %u<br>\n", strlen($this->Buffer)));
58 function serialUInt32 (&$val)
60 if ($this->isReading())
62 $val = ord($this->Buffer{$this->Pos++});
63 $val += ord($this->Buffer{$this->Pos++})*256;
64 $val += ord($this->Buffer{$this->Pos++})*(double)256*256;
65 $val += ord($this->Buffer{$this->Pos++})*(double)256*256*256;
66 debug(sprintf ("read uint32 '%d'<br>\n", $val));
67 // var_dump($val);
69 else
71 debug("D".gettype($this->Buffer)."<br>");
72 $this->Buffer .= chr($val & 0xFF);
73 $this->Buffer .= chr(($val>>8) & 0xFF);
74 $this->Buffer .= chr(($val>>16) & 0xFF);
75 $this->Buffer .= chr(($val>>24) & 0xFF);
76 $this->Pos += 4;
77 debug("E".gettype($this->Buffer)."<br>");
78 debug(sprintf ("write uint32 '%d' %d<br>\n", $val, $this->Pos));
82 function serialString (&$val)
84 if ($this->isReading())
86 $this->serialUInt32($size);
87 debug(sprintf ("read string : size = %u<br>\n", $size));
88 $val = substr ($this->Buffer, $this->Pos, $size);
89 debug(sprintf ("read string '%s'<br>\n", $val));
90 $this->Pos += strlen($val);
92 else
94 $valLen = strlen($val);
95 $this->serialUInt32($valLen);
96 $this->Buffer .= $val;
97 $this->Pos += $valLen;
98 debug(sprintf ("write string '%s' %d<br>\n", $val, $this->Pos));
103 class CMessage extends CMemStream
105 var $MsgName;
107 function setName($name)
109 $this->MsgName = $name;
113 class CCallbackClient
115 var $ConSock = false;
117 var $MsgNum = 0;
119 function connect($addr, $port, &$res)
121 global $SockTimeOut;
123 debug(sprintf("Connect<br>"));
124 $this->MsgNum = 0;
126 $this->ConSock = fsockopen ($addr, $port, $errno, $errstr, $SockTimeOut);
127 debug("H".gettype($this->ConSock)."<br>");
129 if (!$this->ConSock)
131 $res = "Can't connect to the callback server '$addr:$port' ($errno: $errstr)";
133 return false;
135 else
137 // set time out on the socket to 2 secondes
138 stream_set_timeout($this->ConSock, $SockTimeOut);
139 $res = "";
140 return true;
144 function close()
146 if ($this->ConSock)
148 fclose($this->ConSock);
149 debug(sprintf("Close<br>"));
151 else
152 debug(sprintf("Already Closed !<br>"));
155 function sendMessage(&$message)
157 if (!$this->ConSock)
159 debug(sprintf ("Socket is not valid\n"));
160 return false;
162 debug(sprintf ("sendMessage : message Buffer is '%d'<br>\n", $message->Pos));
163 debug(sprintf ("sendMessage : message Buffer is '%d'<br>\n", strlen($message->Buffer)));
164 $hd = new CMemStream;
165 debug(sprintf("SendMessage number %u<br>", $this->MsgNum));
166 $hd->serialUInt32 ($this->MsgNum); // number the packet
167 $this->MsgNum += 1;
168 debug(sprintf("After SendMessage, number %u<br>", $this->MsgNum));
169 $messageType = 0;
170 $hd->serialUInt8 ($messageType);
171 $hd->serialString ($message->MsgName);
173 debug(sprintf ("sendMessage : header size is '%d'<br>\n", $hd->Pos));
175 // $sb .= $message->Buffer;
177 $size = $hd->Pos + $message->Pos;
178 $Buffer = (string) chr(($size>>24)&0xFF);
179 $Buffer .= chr(($size>>16)&0xFF);
180 $Buffer .= chr(($size>>8)&0xFF);
181 $Buffer .= chr($size&0xFF);
182 debug( "E".gettype($hd->Buffer)."<br>");
183 debug("F".gettype($message->Buffer)."<br>");
184 $Buffer .= (string) $hd->Buffer;
185 $Buffer .= (string) $message->Buffer;
187 debug("G".gettype($this->ConSock)."<br>");
189 if (!fwrite ($this->ConSock, $Buffer))
191 debug(sprintf ("Error writing to socket\n"));
192 return false;
194 debug(sprintf ("sent packet size '%d' (written size = %d) <br>\n", strlen($Buffer), $size));
195 fflush ($this->ConSock);
197 return true;
200 function waitMessage()
202 if (!$this->ConSock)
204 debug(sprintf ("Socket is not valid\n"));
205 return false;
209 $size = 0;
210 $val = fread ($this->ConSock, 1);
211 $info = stream_get_meta_data($this->ConSock);
212 if ($info['timed_out'])
214 debug('Connection timed out!');
215 return false;
217 $size = ord($val) << 24;
218 $val = fread ($this->ConSock, 1);
219 $info = stream_get_meta_data($this->ConSock);
220 if ($info['timed_out'])
222 debug('Connection timed out!');
223 return false;
225 $size = ord($val) << 16;
226 $val = fread ($this->ConSock, 1);
227 $info = stream_get_meta_data($this->ConSock);
228 if ($info['timed_out'])
230 debug('Connection timed out!');
231 return false;
233 $size += ord($val) << 8;
234 $val = fread ($this->ConSock, 1);
235 $info = stream_get_meta_data($this->ConSock);
236 if ($info['timed_out'])
238 debug('Connection timed out!');
239 return false;
241 $size += ord($val);
242 debug(sprintf ("receive packet size '%d'<br>\n", $size));
243 $fake = fread ($this->ConSock, 5);
244 $info = stream_get_meta_data($this->ConSock);
245 if ($info['timed_out'])
247 debug('Connection timed out!');
248 return false;
250 $size -= 5; // remove the fake
252 $Buffer = "";
253 while ($size > 0 && strlen($Buffer) != $size)
255 $Buffer .= fread ($this->ConSock, $size - strlen($Buffer));
256 $info = stream_get_meta_data($this->ConSock);
257 if ($info['timed_out'])
259 debug('Connection timed out!');
260 return false;
263 $msgin = new CMemStream;
264 $msgin->setBuffer ($Buffer);
266 // decode msg name
267 $msgin->serialString($name);
269 debug(sprintf("Message name = '%s'<BR>", $name));
270 $message = new CMessage;
271 $message->setBuffer(substr($msgin->Buffer, $msgin->Pos));
272 $message->setName($name);
274 debug(sprintf("In message name = '%s'<br>", $message->MsgName));
276 return $message;
281 // class CSessionManagerProxy
282 // {
283 // function createSession($userId, $sessionType, $callbackClient)
284 // {
285 // debug(sprintf("Creating session for user %u, type %s<BR>", $userId, $sessionType));
286 // $msg = new CMessage;
287 // $msg->setName("CSS");
288 // $msg->serialUInt32($userId);
289 // $msg->serialString($sessionType);
291 // $callbackClient->sendMessage($msg);
292 // }
293 // }
295 // class CSessionManagerClientSkel
296 // {
297 // function waitCallback($callbackClient)
298 // {
299 // $message = $callbackClient->waitMessage();
301 // debug(sprintf("Received message '%s'<BR>", $message->MsgName));
303 // switch($message->MsgName)
304 // {
305 // case "CSSR":
306 // debug(sprintf("Create session result<BR>"));
307 // $this->createSessionResult_skel($message);
308 // break;
310 // case "CSNR":
311 // debug(sprintf("Create scenario result<BR>"));
312 // $this->createScenarioResult_skel($message);
313 // break;
314 // };
315 // }
317 // function createSessionResult_skel($message)
318 // {
319 // $userId = 0;
320 // $sessionId = 0;
321 // $result = false;
323 // $message->serialUInt32($userId);
324 // $message->serialUInt32($sessionId);
325 // $message->serialUInt8($result);
327 // createSessionResult($userId, $sessionId, $result);
328 // }
329 // }
331 // printf("creating callback client...<BR>");
333 // $cb = new CCallbackClient;
334 // $ret = "";
335 // $cb->connect("192.168.0.1", "8060", $ret);
337 // $smp = new CSessionManagerProxy;
339 // printf("creating a new sessions...<BR>");
340 // $smp->createSession(10, "st_edit", $cb);
342 // $smcs = new CSessionManagerClientSkel;
343 // $smcs->waitCallback($cb);
346 // function createSessionResult($userId, $sessionId, $result)
347 // {
348 // echo "The session result for user $userId is the session $sessionId with a result of $result\n";
349 // }
352 // This function connect to the AS.
353 // If true, $res contains the url to connect.
354 // If false, $res contains the reason why it s not okay.
356 // function connectToAS(&$fp, &$res)
357 // {
358 // global $ASHost, $ASPort;
359 ///*
360 // $sid = session_id();
361 // $result = sqlquery("SELECT socket_id FROM resident_socket");
362 // if (!$result || sqlnumrows($result) == 0)
363 // {
364 // $fp = pfsockopen ($ASHost, $ASPort, $errno, $errstr, 30);
365 // echo "opened resident socket '$fp'\n";
367 // $result = sqlquery("SELECT socket_id FROM resident_socket WHERE socket_id='$fp'");
368 // if ($result && sqlnumrows($result)>0)
369 // sqlquery("DELETE FROM resident_socket WHERE socket_id='$fp'");
371 // sqlquery("INSERT INTO resident_socket SET socket_id='$fp', session_id='$sid', last_access=NOW()");
372 // }
373 // else
374 // {
375 // $result = sqlfetch($result);
376 // $fp = $result["socket_id"];
377 // }
379 // // remove too old sockets
380 // sqlquery("SELECT socket_id FROM resident_socket WHERE NOW()-last_access > 1800");
381 // while ($result && ($arr=sqlfetch($result)))
382 // {
383 // fclose((int)($arr["socket_id"]));
384 // sqlquery("DELETE FROM resident_socket WHERE socket_id='".$arr["socket_id"]."'");
385 // }
387 // // update current socket last access
388 // sqlquery("UPDATE resident_socket SET last_access=NOW() WHERE socket_id='$fp' AND session_id='$sid'");
389 //*/
391 // // connect to the login service that must be $ASHost:$ASPort
392 // $fp = fsockopen ($ASHost, $ASPort, $errno, $errstr, 30);
393 // if (!$fp)
394 // {
395 // $res = "Can't connect to the admin service '$ASHost:$ASPort' ($errno: $errstr)";
396 // }
397 // else
398 // {
399 // $res = "";
400 // }
402 // }
404 // function disconnectFromAS(&$fp)
405 // {
406 ///*
407 // $result = sqlquery("SELECT socket_id FROM resident_socket WHERE socket_id='$fp'");
408 // if (!$result || sqlnumrows($socket)==0)
409 // fclose($fp);
410 //*/
411 // fclose($fp);
412 // }
414 // function sendMessage ($fp, $msgout)
415 // {
416 // $size = $msgout->Pos;
417 // $Buffer = chr(($size>>24)&0xFF);
418 // $Buffer .= chr(($size>>16)&0xFF);
419 // $Buffer .= chr(($size>>8)&0xFF);
420 // $Buffer .= chr($size&0xFF);
421 // $Buffer .= $msgout->Buffer;
423 // fwrite ($fp, $Buffer);
425 // //printf ("sent packet size '%d'<br>", strlen($Buffer));
427 // fflush ($fp);
428 // }
430 // function waitMessage ($fp, &$msgin)
431 // {
432 // //echo "waiting a message";
433 // $size = 0;
434 // $val = fread ($fp, 1);
435 // $size = ord($val) << 24;
436 // $val = fread ($fp, 1);
437 // $size = ord($val) << 16;
438 // $val = fread ($fp, 1);
439 // $size += ord($val) << 8;
440 // $val = fread ($fp, 1);
441 // $size += ord($val);
442 // //printf ("receive packet size '%d'<br>", $size);
443 // $fake = fread ($fp, 4);
444 // $size -= 4; // remove the fake
446 // $Buffer = fread ($fp, $size);
447 // $msgin = new CMemStream;
448 // $msgin->setBuffer ($Buffer);
449 // }
451 // function logNelQuery($query)
452 // {
453 // global $uid;
454 ///*
455 // $f = fopen("./nel_queries.log", "a");
456 // fwrite($f, date("Y/m/d H:i:s")." ".sprintf("%-16s", $admlogin)." $query\n");
457 // fclose($f);
458 //*/
460 // logUser($uid, "QUERY=".$query);
461 // }
463 // function nel_query($rawvarpath, &$result)
464 // {
465 // global $nel_queries;
467 // $nel_queries[] = $rawvarpath;
468 // $ok = false;
469 // //echo "rawvarpath=$rawvarpath<br>\n";
471 // //logNelQuery($rawvarpath);
473 // connectToAS($fp, $result);
474 // if(strlen($result) != 0)
475 // return $ok;
477 // // send the message that say that we want to add a user
478 // $msgout = new CMemStream;
479 // $fake = 0;
480 // $msgout->serialuint32 ($fake); // fake used to number the packet
481 // $messageType = 0;
482 // $msgout->serialuint8 ($messageType);
483 // $msgout->serialstring ($rawvarpath);
485 // sendMessage ($fp, $msgout);
487 // waitMessage ($fp, $msgin);
489 // $msgin->serialstring($result);
491 // if(strlen($result) == 0)
492 // {
493 // // it failed
494 // }
495 // else
496 // {
497 // // it's ok
498 // $ok = true;
499 // }
501 // //printf("receive response '$result'<br>\n");
503 // disconnectFromAS(&$fp);
504 // //echo "sent OK.<br><br>\n";
506 // return $ok;
507 // }