copyright updated and mop user status added
[brisk.git] / web / index_wr.php
1 <?php
2 /*
3  *  brisk - index_wr.php
4  *
5  *  Copyright (C) 2006-2011 Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it 
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details. You should have received a
19  * copy of the GNU General Public License along with this program; if
20  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21  * Suite 330, Boston, MA 02111-1307, USA.
22  *
23  */
24
25 require_once("Obj/brisk.phh");
26 require_once("Obj/auth.phh");
27 // require_once("Obj/proxyscan.phh");
28
29 // Use of proxies isn't allowed.
30 // if (is_proxy()) {
31 //   sleep(5);
32 //   exit;
33 // }
34
35 $mlang_indwr = array( 'btn_backtotab' => array( 'it' => 'Torna ai tavoli.',
36                                                 'en' => 'Back to tables.' ),
37                       'warrrepl'  => array( 'it' => '<br>Il nominativo &egrave; stato inoltrato all\'amministratore.<br><br>Nell\'arco di pochi giorni verr&agrave;<br><br>notificata al garantito l\'avvenuta registrazione.',
38                                             'en' => '<br>The subscription was forwarded to the administrator.<br><br>In a few days we will notify<br><br>your friend the occurred registration.'),
39                       'btn_close' => array( 'it' => 'chiudi',
40                                             'en' => 'close' ),
41                       'commerr' => array( 'it' => '<b>E\' occorso un errore durante il salvataggio, riprova o contatta l\'amministratore.</b>',
42                                           'en' => '<b>An error was occurred during the saving, try again or contact the administrator.</b>'),
43                       'warrmust' => array( 'it' => '<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>',
44                                            'en' => 'To authenticate somebody you have to be authenticated in your turn'),
45                       'mesgrepl' => array( 'it' => '<br><br>Il messaggio &egrave; stato inoltrato all\'amministratore.',
46                                            'en' => '<br><br>The message was forwarded to the administrator'),
47                       'mesgmust' => array( 'it' => '<b>Per mandare messaggi all\'amministratore devi essere autenticato.</b>',
48                                            'en' => 'To send a message to the administrator you have to be authenticated'),
49                       'shutmsg'  => array( 'it' => '<b>Il server sta per essere riavviato, non possono avere inizio nuove partite.</b>',
50                                            'en' => '<b>The server is going to be rebooted, new games are not allowed.</b>'),
51                       'mustauth' => array( 'it' => '<b>Il tavolo a cui volevi sederti richiede autentifica.</b>',
52                                            'en' => '<b>the table where you want to sit require authentication</b>'),
53                       'tabwait_a'=> array( 'it' => '<b>Il tavolo si &egrave; appena liberato, ci si potr&agrave; sedere tra ',
54                                            'en' => '<b>The table is only just opened, you will sit down in '), // FIXME
55                       'tabwait_b'=> array( 'it' => ' secondi.</b>',
56                                            'en' => ' seconds.</b>'),
57                       'pollmust' => array( 'it' => '<b>Per partecipare al sondaggio devi essere autenticato.</b>',
58                                            'en' => '<b>To vote for the poll you have to be authenticated</b>'),
59                       'pollnone' => array( 'it' => '<br><br>Al momento non è attivo alcun sondaggio.',
60                                            'en' => '<br><br>At this moment no polls are active.'),
61                       'pollchoo' => array( 'it' => '<br><br>Non hai espresso nessuna preferenza.',
62                                            'en' => '<br><br>You don\'t choose any preference, do it'), 
63                       'pollagai' => array( 'it' => '<br>Per questo sondaggio hai già votato.<br><br>Non si può esprimere la propria preferenza più di una volta.',
64                                            'en' => '<br>You just express your preference about this poll.<br><br>You cannot do it again.'),
65                       'pollrec'  => array ('it' => '<br><br>Il tuo voto è stato registrato.',
66                                            'en' => '<br><br>Your vote had be stored.'),
67                       'badwake_a'=> array( 'it' => '<br>Ti sei alzato da un tavolo senza il consenso degli altri giocatori.<br><br>Dovrai aspettare ancora ',
68                                            'en' => '<br>You stand up without the permission of the other players.<br><br>You will wait '),
69                       'badwake_b'=> array( 'it' => ' prima di poterti sedere nuovamente.',
70                                            'en' => ' before you can sit down again.'),
71                       'btn_stays'=> array( 'it' => 'resta in piedi.',
72                                            'en' => 'stay standing.'),
73                       'badsit_a' => array( 'it' => '<br>Tu o qualcuno col tuo stesso indirizzo IP si è alzato da un tavolo senza il consenso degli altri giocatori.<br><br>Dovrai aspettare ancora ',
74                                            'en' => '<br>You or someone with your same IP address is standing up from a table without the permission of the other players <br><br>You will wait '), 
75                       'badsit_b' => array( 'it' => ' prima di poterti sedere nuovamente.<br><br>Se non sei stato tu ad alzarti e possiedi un login con password, autenticandoti con quello, potrai accedere.',
76                                            'en' => ' before you can sit down again. If you don\'t leave the table and you have a login with a password, authenticating with this one you will access')
77
78                       );
79
80 log_load("index_wr.php");
81
82 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
83   echo "Debugging time!";
84   exit;
85 }
86
87 /*
88  *  MAIN
89  */
90
91 /* if the IP is banned, exit without do nothing */
92 if (array_search($_SERVER['REMOTE_ADDR'], $G_black_list) !== FALSE) {
93   sleep(5);
94   exit;
95 }
96
97 $is_spawn = FALSE;
98
99 log_mop(0, 'index_wr.php: COMM: '.xcapemesg($mesg));
100 log_wr('COMM: '.xcapemesg($mesg));
101
102 $sem = Room::lock_data();
103 if (($room = &Room::load_data()) == FALSE) {
104   echo "Load data error";
105   log_wr("Load data error");
106   Room::unlock_data($sem);
107   exit;
108 }
109 if (($user = &$room->get_user($sess, &$idx)) == FALSE) {
110   Room::unlock_data($sem);
111   $argz = explode('|', xcapemesg($mesg));
112
113   if ($argz[0] == 'getchallenge') {
114     GLOBAL $cli_name;
115     if (($a_sem = Challenges::lock_data()) != FALSE) { 
116       log_main("chal lock data success");
117       
118       if (($chals = &Challenges::load_data()) != FALSE) {
119         $curtime = time();
120
121         $token =  uniqid("");
122         // echo '2|'.$argz[1].'|'.$token.'|'.$_SERVER['REMOTE_ADDR'].'|'.$curtime.'|';
123         // exit;
124
125         if (($login_new = validate_name(urldecode($cli_name))) != FALSE) {
126           if ($chals->add($login_new, $token, $_SERVER['REMOTE_ADDR'], $curtime) != FALSE) {
127             echo '0|'.$token;
128           }
129           else {
130             echo '1|';
131           }
132         }
133         else {
134           echo '1|';
135         }
136         if ($chals->ismod()) {
137           Challenges::save_data(&$chals);
138         }
139       }
140       
141
142       Challenges::unlock_data($a_sem);
143     }
144   }
145   else if ($argz[0] == 'auth') {
146     printf("challenge|ok");
147   }
148   else if ($argz[0] == 'help') {
149     /* MLANG: "torna ai tavoli" */ 
150     echo show_notify(str_replace("\n", " ", $G_room_help[$G_lang]), 0, $mlang_indwr['btn_close'][$G_lang], 600, 500);
151   }
152   else if ($argz[0] == 'about') {
153     echo show_notify(str_replace("\n", " ", $G_room_about[$G_lang]), 0, $mlang_indwr['btn_close'][$G_lang], 400, 220);
154   }
155   else if ($argz[0] == 'passwdhowto') {
156     echo show_notify(str_replace("\n", " ", $G_room_passwdhowto[$G_lang]), 0, $mlang_indwr['btn_close'][$G_lang], 400, 200);
157   }
158   else if ($argz[0] == 'roadmap') {
159     echo show_notify(str_replace("\n", " ", $G_room_roadmap[$G_lang]), 0, $mlang_indwr['btn_close'][$G_lang], 400, 200);
160   }
161   else if ($argz[0] == 'placing') {
162     GLOBAL $G_false;
163
164     require_once("briskin5/Obj/briskin5.phh");
165     require_once("briskin5/Obj/placing.phh");
166
167     echo show_notify(str_replace("\n", " ", placings_show($G_false)), 0, $mlang_indwr['btn_close'][$G_lang], 800, 600);
168   }
169   else if ($argz[0] == 'whysupport') {
170     echo show_notify(str_replace("\n", " ", $G_room_whysupport[$G_lang]), 0, $mlang_indwr['btn_close'][$G_lng], 400, 200);
171   }
172   else { 
173     log_wr("Get User Error");
174     echo "Get User Error:" + $argz[0];
175   }
176   exit;
177 }
178 $argz = explode('|', xcapemesg($mesg));
179
180 log_wr('POSTSPLIT: '.$argz[0]);
181
182 log_mop($user->step, 'index_wr.php: after get_user()');
183
184 if ($argz[0] == 'shutdown') {
185   log_auth($user->sess, "Shutdown session.");
186
187   $user->reset();
188
189   log_rd2("AUTO LOGOUT.");
190   if ($user->subst == 'sitdown' || $user->stat == 'table')
191     $room->room_wakeup(&$user);
192   else if ($user->subst == 'standup')
193     $room->room_outstandup(&$user);
194   else {
195     log_rd2("SHUTDOWN FROM WHAT ???");
196   }
197 }
198 else if ($argz[0] == 'warranty') {
199   GLOBAL $cli_name, $cli_email;
200
201   $curtime = time();
202   $mesg_to_user = "";
203
204   log_wr("INFO:SKIP:argz == warranty name: [".$cli_name."] AUTH: ".($user->flags & USER_FLAG_AUTH));
205   if ($user->flags & USER_FLAG_AUTH) {
206     if (($wa_lock = Warrant::lock_data()) != FALSE) {
207       if (($fp = @fopen(LEGAL_PATH."/warrant.txt", 'a')) != FALSE) {
208         /* Unix time | session | nickname | IP | where was | mesg */
209         fwrite($fp, sprintf("%ld|%s|%s|%s|\n", $curtime, $user->name, xcapelt(urldecode($cli_name)), xcapelt(urldecode($cli_email))));
210         fclose($fp);
211       }
212       Warrant::unlock_data($wa_lock);
213       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
214       /* MLANG: "<br>Il nominativo &egrave; stato inoltrato all\'amministratore.<br><br>Nell\'arco di pochi giorni vi verr&agrave;<br><br>notificata l\'avvenuta registrazione." */
215       $user->comm[$user->step % COMM_N] .=  show_notify($mlang_indwr['warrrepl'][$G_lang], 0, $mlang_indwr['btn_close'][$G_lang], 400, 150);
216       $user->step_inc();
217       echo "1";
218     }
219     else {
220       /* MLANG: "<b>E\' occorso un errore durante il salvataggio, riprova o contatta l\'amministratore.</b>" */
221       $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_indwr['commerr'][$G_lang]);
222     }
223     
224   }
225   else {
226     /* MLANG: "<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>" */
227     $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_indwr['warrmust'][$G_lang]);
228   }
229
230   if ($mesg_to_user != "") {
231     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
232     
233     $dt = date("H:i ", $curtime);
234     $user->comm[$user->step % COMM_N] .= $mesg_to_user;
235     $user->step_inc();
236   }
237 }
238 else if ($argz[0] == 'mesgtoadm') {
239   GLOBAL $cli_subj, $cli_mesg;
240
241   $curtime = time();
242   $mesg_to_user = "";
243
244   log_wr("INFO:SKIP:argz == mesgtoadm name: [".$cli_name."] AUTH: ".($user->flags & USER_FLAG_AUTH));
245   if ($user->flags & USER_FLAG_AUTH) {
246     if (($wa_lock = Warrant::lock_data()) != FALSE) {
247       $userdb = new LoginDB();
248       
249       if (($ema = $userdb->getmail($user->name)) != FALSE) {
250         //  mail("nastasi", 
251         mail("brisk@alternativeoutput.it", urldecode($cli_subj), urldecode($cli_mesg), sprintf("From: %s <%s>", $user->name, $ema));
252       }
253
254       if (($fp = @fopen(LEGAL_PATH."/messages.txt", 'a')) != FALSE) {
255         /* Unix time | session | nickname | IP | where was | mesg */
256         fwrite($fp, sprintf("%ld|%s|%s|%s\n", $curtime, $user->name, 
257                             xcapelt(urldecode($cli_subj)), xcapelt(urldecode($cli_mesg))));
258         fclose($fp);
259       }
260       Warrant::unlock_data($wa_lock);
261       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
262       /* MLANG: "" */
263       $user->comm[$user->step % COMM_N] .=  show_notify($mlang_indwr['mesgrepl'][$G_lang], 0, $mlang_indwr['btn_close'][$G_lang], 400, 110);
264       $user->step_inc();
265       echo "1";
266     }
267     else {
268       /* MLANG: "<b>E\' occorso un errore durante il salvataggio, riprova o contatta l\'amministratore.</b>" */
269       $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_indwr['commerr'][$G_lang]);
270     }
271     
272   }
273   else {
274     /* MLANG: "<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>" */
275     $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_indwr['mesgmust'][$G_lang]);
276   }
277
278   if ($mesg_to_user != "") {
279     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
280     
281     $dt = date("H:i ", $curtime);
282     $user->comm[$user->step % COMM_N] .= $mesg_to_user;
283     $user->step_inc();
284   }
285 }
286
287
288
289 else if ($argz[0] == 'poll') {
290   GLOBAL $G_with_poll, $G_poll_name, $cli_choose, $cli_poll_name;
291
292   $poll_lock = FALSE;
293   $curtime = time();
294   $mesg_to_user = "";
295   
296   $fp = FALSE;
297   $echont = "0";
298
299   /*
300           DONE - autorizzato ?
301           DONE - ci sono poll attivi ?
302           - verifica che il poll_name del client sia uguale a quello sul server
303           DONE - lock
304           DONE - apro file r+ con fallback in w+
305           DONE - vedo se ha già votato
306           DONE - se si: messaggio di voto già dato
307           se no: accetto il voto e lo segno; messaggio
308           chiudo file
309   */
310
311   $dobreak = FALSE;
312   do {
313     log_wr("INFO:SKIP:argz == poll name: [".$cli_name."] AUTH: ".($user->flags & USER_FLAG_AUTH));
314     if (($user->flags & USER_FLAG_AUTH) != USER_FLAG_AUTH) {
315       // MLANG: <b>Per partecipare al sondaggio devi essere autenticato.</b>
316       $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_indwr['pollmust'][$G_lang]);
317       log_wr("break1");
318       break;
319     }
320
321     if ($G_with_poll == FALSE && $G_poll_name != FALSE && $G_poll_name != "") {
322       $mesg_to_user = show_notify($mlang_indwr['pollnone'][$G_lang], 0, $mlang_indwr['btn_close'][$G_lang], 400, 110);
323       log_wr("break2");
324       break;
325     }
326     
327     if ($cli_choose == "" || !isset($cli_choose)) {
328       $mesg_to_user = show_notify($mlang_indwr['pollchoo'][$G_lang], 0, $mlang_indwr['btn_close'][$G_lang], 400, 110);
329       log_wr("break2.5");
330       break;
331     }
332     
333     if (($poll_lock = Poll::lock_data()) == FALSE) {
334       /* MLANG: "<b>E\' occorso un errore durante il salvataggio, riprova o contatta l\'amministratore.</b>" */
335       $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_indwr['commerr'][$G_lang]);
336       log_wr("break3");
337       break;
338     }
339     
340     if (($fp = @fopen(LEGAL_PATH."/".$G_poll_name.".txt", 'r+')) == FALSE) 
341       $fp = @fopen(LEGAL_PATH."/".$G_poll_name.".txt", 'w+');
342     
343     if ($fp == FALSE) {
344       $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_indwr['commerr'][$G_lang]);
345       log_wr("break4");
346       break;
347     }
348     
349     log_wr("poll: cp");
350     fseek($fp, 0);
351     
352     log_wr("poll: cp2");
353     while (!feof($fp)) {
354       log_wr("poll: cp3");
355       $bf = fgets($fp, 4096);
356       log_wr("poll: cp3.1");
357       $arli = csplitter($bf, '|');
358       if (count($arli) == 0)
359         break;
360     log_wr("poll: cp3.2");
361       if (strcasecmp($arli[1], $user->name) == 0) {
362         $mesg_to_user = show_notify($mlang_indwr['pollagai'][$G_lang], 0, $mlang_indwr['btn_close'][$G_lang], 400, 110);
363         $dobreak = TRUE;
364         break;
365       }
366     }
367     log_wr("poll: cp4");
368
369     if ($dobreak) {
370       log_wr("break5");
371       break;
372     }
373       
374     /* Unix time | nickname | choose */
375     fwrite($fp, sprintf("%ld|%s|%s\n", $curtime, xcapelt($user->name), xcapelt(urldecode($cli_choose))));
376     fflush($fp);
377     $mesg_to_user =  show_notify($mlang_indwr['pollrec'][$G_lang], 0, $mlang_indwr['btn_close'][$G_lang], 400, 110);
378     $echont = "1";
379     log_wr("poll: cp5");
380   } while (0);
381
382   if ($fp != FALSE)
383     fclose($fp);
384
385   if ($poll_lock != FALSE)
386     Poll::unlock_data($poll_lock);
387   
388   if ($mesg_to_user != "") {
389     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
390     
391     $dt = date("H:i ", $curtime);
392     $user->comm[$user->step % COMM_N] .= $mesg_to_user;
393     $user->step_inc();
394   }
395
396   echo "$echont";
397 }
398
399 /******************
400  *                *
401  *   STAT: room   *
402  *                *
403  ******************/
404 else if ($user->stat == 'room') {
405   $user->laccwr = time();
406
407   if ($argz[0] == 'help') {
408     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
409     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_help[$G_lang]), 0, $mlang_indwr['btn_backtotab'][$G_lang], 600, 500);
410
411     log_wr($user->comm[$user->step % COMM_N]);
412     $user->step_inc();
413     
414   }
415   else if ($argz[0] == 'passwdhowto') {
416     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
417     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_passwdhowto[$G_lang]), 0, $mlang_indwr['btn_backtotab'][$G_lang], 600, 500);
418
419     log_wr($user->comm[$user->step % COMM_N]);
420     $user->step_inc();
421     
422   }
423   else if ($argz[0] == 'splash') {
424     GLOBAL $G_with_splash, $G_splash_content, $G_splash_interval, $G_splash_idx;
425     GLOBAL $G_splash_w, $G_splash_h, $G_splash_timeout;
426     $CO_splashdate = "CO_splashdate".$G_splash_idx;
427     GLOBAL $$CO_splashdate;
428
429     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
430
431
432     $user->comm[$user->step % COMM_N] .=  show_notify_ex(str_replace("\n", " ", $G_splash_content[$G_lang]), 0, $mlang_indwr['btn_backtotab'][$G_lang], $G_splash_w, $G_splash_h, true, 0);
433
434     log_wr($user->comm[$user->step % COMM_N]);
435     $user->step_inc();
436   }
437   else if ($argz[0] == 'about') {
438     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
439     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_about[$G_lang]), 0, $mlang_indwr['btn_backtotab'][$G_lang], 400, 200);
440
441     log_wr($user->comm[$user->step % COMM_N]);
442     $user->step_inc();
443     
444   }
445
446
447   else if ($argz[0] == 'placing') {
448
449     require_once("briskin5/Obj/briskin5.phh");
450     require_once("briskin5/Obj/placing.phh");
451
452     $user->comm[$user->step % COMM_N] =  "gst.st = ".($user->step+1)."; ";
453     $user->comm[$user->step % COMM_N] .= show_notify_ex(str_replace("\n", " ", placings_show($user)), 0, $mlang_indwr['btn_backtotab'][$G_lang], 800, 600, TRUE, 0);
454
455     log_wr($user->comm[$user->step % COMM_N]);
456     $user->step_inc();
457
458
459   }
460
461
462   else if ($argz[0] == 'roadmap') {
463     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
464     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_roadmap[$G_lang]), 0, $mlang_indwr['btn_backtotab'][$G_lang], 400, 200);
465
466     log_wr($user->comm[$user->step % COMM_N]);
467     $user->step_inc();
468     
469   }
470   else if ($argz[0] == 'whysupport') {
471     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
472     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_whysupport[$G_lang]), 0, $mlang_indwr['btn_backtotab'][$G_lang], 400, 200);
473
474     log_wr($user->comm[$user->step % COMM_N]);
475     $user->step_inc();
476     
477   }
478   else if ($argz[0] == 'chatt') {
479     $room->chatt_send(&$user, xcapemesg($mesg));
480   }
481   /**********************
482    *                    *
483    *   SUBST: standup   *
484    *                    *
485    **********************/
486   else if ($user->subst == 'standup') {
487    
488     if ($argz[0] == 'sitdown') {
489       log_wr("SITDOWN command");
490
491       if ($user->the_end == TRUE) {
492         log_wr("INFO:SKIP:argz == sitdown && the_end == TRUE => ignore request.");
493         Room::unlock_data($sem);
494         exit;
495       }
496
497       // Take parameters
498       $table_idx = $argz[1];
499       $table = &$room->table[$table_idx];
500     
501       $curtime = time();
502
503       if ($G_shutdown || $table->wakeup_time > $curtime || 
504           ($table->auth_only && (($user->flags & USER_FLAG_AUTH) == 0)) ) {
505         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
506
507         $dt = date("H:i ", $curtime);
508         /* MLANG: "<b>Il server sta per essere riavviato, non possono avere inizio nuove partite.</b>", "<b>Il tavolo a cui volevi sederti richiede autentifica.</b>", "<b>Il tavolo si &egrave; appena liberato, ci si potr&agrave; sedere tra %d secondi.</b>" */
509         if ($G_shutdown) {
510           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_indwr['shutmsg'][$G_lang]);
511         }
512         else if ($table->auth_only && (($user->flags & USER_FLAG_AUTH) == 0)) {
513           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_indwr['mustauth'][$G_lang]);
514         }
515         else {
516           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"%s%d%s");', $dt, NICKSERV, $mlang_indwr['tabwait_a'][$G_lang], $table->wakeup_time - $curtime, $mlang_indwr['tabwait_b'][$G_lang]);
517         }
518         $user->step_inc();
519         Room::save_data($room);
520         Room::unlock_data($sem);
521         exit;
522       }
523
524       /* TODO: refact to a function */
525       // if ($user->bantime > $user->laccwr) {
526       require_once("Obj/hardban.phh");
527
528       if (($bantime = Hardbans::check(($user->flags & USER_FLAG_AUTH ? $user->name : FALSE),
529                           $user->ip, $user->sess)) != -1) {
530         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
531         /* MLANG: "<br>Ti sei alzato da un tavolo senza il consenso degli altri giocatori. <br><br>Dovrai aspettare ancora ".secstoword($user->bantime - $user->laccwr)." prima di poterti sedere nuovamente.", "resta in piedi.", "<br>Tu o qualcuno col tuo stesso indirizzo IP si è alzato da un tavolo senza il consenso degli altri giocatori.<br><br>Dovrai aspettare ancora ".secstoword($bantime - $user->laccwr)." prima di poterti sedere nuovamente.<br><br>Se non sei stato tu ad alzarti e possiedi un login con password, autenticandoti con quello, potrai accedere." */
532         if ($user->flags & USER_FLAG_AUTH) {
533           $user->comm[$user->step % COMM_N] .= show_notify($mlang_indwr['badwake_a'][$G_lang].secstoword($user->bantime - $user->laccwr).$mlang_indwr['badwake_b'][$G_lang], 2000, $mlang_indwr['btn_stays'][$G_lang], 400, 100);
534         }
535         else {
536           $user->comm[$user->step % COMM_N] .= show_notify($mlang_indwr['badsit_a'][$G_lang].secstoword($bantime - $user->laccwr).$mlang_indwr['badsit_a'][$G_lang], 2000, $mlang_indwr['btn_stays'][$G_lang], 400, 180);
537         }
538         $user->step_inc();
539         Room::save_data($room);
540         Room::unlock_data($sem);
541         exit;
542       }
543     
544       if ($table->player_n == PLAYERS_N) {
545         log_wr("WARN:FSM: Sitdown unreachable, table full.");
546         Room::unlock_data($sem);
547         exit;
548       } 
549       
550       // set new status
551       $user->subst = "sitdown";
552       $user->table = $table_idx;
553       $user->table_pos = $table->user_add($idx);
554       
555       log_wr("MOP before");
556
557       if ($table->player_n == PLAYERS_N) {
558         require_once("briskin5/Obj/briskin5.phh");
559         log_wr("MOP inall");
560
561         // Start game for this table.
562         log_wr("Start game!");
563         
564         //
565         //  START THE SPAWN HERE!!!!
566         //
567
568         $curtime = time();
569
570         // Create new spawned table
571         $bri_sem = Briskin5::lock_data($table_idx);
572         $table_token = uniqid("");
573         $room->table[$table_idx]->table_token = $table_token;
574         $room->table[$table_idx]->table_start = $curtime;
575         
576         $plist = "$table_token|$user->table|$table->player_n";
577         for ($i = 0 ; $i < $table->player_n ; $i++) {
578           $plist .= '|'.$room->user[$table->player[$i]]->sess;
579         }
580         log_legal($curtime, $user, "STAT:CREATE_GAME", $plist);
581
582         if (($bri =& new Briskin5(&$room, $table_idx, $table_token)) == FALSE)
583           log_wr("bri create: FALSE");
584         else
585           log_wr("bri create: ".serialize($bri));
586         
587         // init table
588         $bri_table =& $bri->table[0];
589         $bri_table->init(&$bri->user);
590         $bri_table->game_init(&$bri->user);
591         //
592         // Init spawned users.
593         //
594         //  MULTIGAME: here init of selected game instead of hardcabled briskin5 init (look subst status)
595         // 
596         log_wr("game_init after");
597         for ($i = 0 ; $i < $table->player_n ; $i++) {
598           $bri_user_cur = &$bri->user[$i];
599           $user_cur = &$room->user[$table->player[$i]];
600           
601           $bri_user_cur->stat_set('table');
602           $bri_user_cur->subst = 'asta';
603           $bri_user_cur->laccwr = $curtime;
604           
605           $bri_user_cur->trans_step = $user_cur->step + 1;
606           $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = "";
607           $bri_user_cur->step_inc();
608           $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = show_table(&$bri,&$bri_user_cur,$bri_user_cur->step+1,TRUE, FALSE);
609           
610           $bri_user_cur->step_inc();
611           
612           log_wr("TRY PRESAVE: ".$bri_user_cur->step." TRANS STEP: ".$bri_user_cur->trans_step);
613           
614           log_wr("Pre if!");
615           
616           //          ARRAY_POP DISABLED
617           //        // CHECK
618           while (array_pop($user_cur->comm) != NULL);
619           
620           $ret = "";
621           $ret .= sprintf('gst.st_loc++; gst.st=%d; createCookie("table_idx", %d, 24*365, cookiepath); createCookie("table_token", "%s", 24*365, cookiepath); createCookie("lang", "%s", 24*365, cookiepath); the_end=true; window.onunload = null ; window.onbeforeunload = null ; document.location.assign("briskin5/index.php");|', $user_cur->step+1, $table_idx, $table_token, $G_lang);
622           
623           $user_cur->comm[$user_cur->step % COMM_N] = $ret;
624           $user_cur->trans_step = $user_cur->step + 1;
625           log_wr("TRANS ATTIVATO");
626           
627           
628           $user_cur->stat_set('table');
629           $user_cur->subst = 'asta';
630           $user_cur->laccwr = $curtime;
631           $user_cur->step_inc();
632         }
633         log_wr("presave bri");
634         Briskin5::save_data($bri);
635         Briskin5::unlock_data($bri_sem);
636         log_wr("postsave bri");
637       }
638       // change room
639       $room->room_sitdown(&$user, $table_idx);
640
641       log_wr("MOP finish");
642
643       
644     }
645     else if ($argz[0] == 'logout') {
646       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
647       $user->comm[$user->step % COMM_N] .= 'postact_logout();';
648       $user->the_end = TRUE;
649       $user->step_inc();
650     }
651   }
652   /**********************
653    *                    *
654    *   SUBST: sitdown   *
655    *                    *
656    **********************/
657   else if ($user->subst == 'sitdown') {
658     if ($argz[0] == 'wakeup') {
659       $room->room_wakeup(&$user);      
660     }
661     else if ($argz[0] == 'logout') {
662       $room->room_wakeup(&$user);      
663       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
664       $user->comm[$user->step % COMM_N] .= 'postact_logout();';
665       $user->the_end = TRUE;
666       $user->step_inc();
667     }
668   }
669 }
670 log_wr("before save data");
671 Room::save_data($room);
672 log_mop($user->step, 'index_wr.php: after save_data()');
673
674 Room::unlock_data($sem);
675 exit;
676 ?>