game related functions moved to briskin5, begin of multilang support, switch user...
[brisk.git] / web / Obj / brisk.phh
1 <?php
2 /*
3  *  brisk - brisk.phh
4  *
5  *  Copyright (C) 2006-2008 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
26 define(BRISK_CONF, "brisk.conf.pho");
27 define(FTOK_PATH, "/var/lib/brisk");
28 define(LEGAL_PATH, "/tmp/legal_brisk");
29 define(PROXY_PATH, "/var/lib/brisk_proxy");
30 define(TABLES_N, 32);
31 define(PLAYERS_N, 3);
32 define(MAX_POINTS, 5);
33 define(MAX_PLAYERS, (20 + (PLAYERS_N * TABLES_N)));
34 define(SHM_DIMS_MIN, (50000 + 10000 * TABLES_N + 15000 * MAX_PLAYERS));
35 define(SHM_DIMS_MAX, SHM_DIMS_MIN + 1048576);
36 define(SHM_DIMS_DLT, 65536);
37  
38 define(COMM_N, 18);
39 define(COMM_GEN_N, 50);
40
41 define(CHAT_N, 3);
42 define(CHAT_ILL_TIME, 6);
43
44 define(SESS_LEN, 13);
45 define(STREAM_TIMEOUT, 20);
46 define(EXPIRE_TIME_RD, 180);
47 define(EXPIRE_TIME_SMAMMA, 360); 
48 define(EXPIRE_TIME_WAG, 10);
49 define(WAKEUP_TIME, 12); 
50 // BAN_TIME da allineare anche in commons.js
51 define(BAN_TIME, 3600); 
52 define(GARBAGE_TIMEOUT, 10);
53 define(NICKSERV, "<i>BriskServ</i>");
54
55 define(DBG_ONL2, 0x0001);
56 define(DBG_ONLY, 0x0002);
57 define(DBG_MAIN, 0x0004);
58 define(DBG_READ, 0x0008);
59 define(DBG_REA2, 0x0010);
60 define(DBG_SEND, 0x0020);
61 define(DBG_LOCK, 0x0040);
62 define(DBG_WRIT, 0x0080);
63 define(DBG_LOAD, 0x0100);
64 define(DBG_AUTH, 0x0200);
65 define(DBG_CRIT, 0x0400);
66
67 // NOTE: BRISK DEBUG must be a numerical constant, not the result of operations on symbols 
68 define(BRISK_DEBUG, 0xffffffff);
69
70 define(BRISK_SINGLE_DEBUG,0);
71 define(BRISK_SINGLE_SESS, "");
72 // define(DEBUGGING, "local");
73
74 require_once("$DOCUMENT_ROOT/Etc/".BRISK_CONF);
75
76 $G_false = FALSE;
77 $G_lang = "it";
78 $G_lng = "";
79 // $G_lang = "en";
80 // $G_lng = "_en";
81
82 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
83 $G_brisk_version = "2.1.5 - trusty";
84
85 /* MLANG: ALL THE INFO STRINGS IN brisk.phh */
86 $root_wellarr = Array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: il rendering degli utenti in piedi &egrave; stato riscritto.',
87                         'Se vuoi iscriverti alla <a target="_blank" href="http://www.milug.org/cgi-bin/mailman/listinfo/ml-briscola">Mailing List</a>, cliccala!' );
88
89 $G_room_help= '
90 <div style=\\"text-align: left; padding: 8px;\\">
91 <b>Descrizione</b><br>
92 Questa รจ un\'implementazione della briscola in cinque, cos&igrave; come &egrave; spiegata su
93 <a target=\\"_blank\\" href=\\"http://it.wikipedia.org/wiki/Briscola#Gioco_a_5\\">Wikipedia</a>; in breve &egrave; la variante con l\'asta prima sulla carta e poi sui punti.<br><br>
94 <b>Configurazione del browser.</b><br>
95 Occorre abilitare i cookies.<br>
96 <br>
97 <b>Uso del sito</b><br>
98 Potete sedervi a un tavolo o rimanere in piedi.<br>
99 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
100 <br>
101 <b>Partita</b><br>
102 All\'inizio vengono distribuite le carte e parte l\'asta; per partecipare all\'asta, quando sar&agrave; il vostro turno, potrete scegliere se andare avanti o passare cliccando sulle icone corrispondenti. Se si arriva ai punti, scrivete nella textbox il vostro rilancio e cliccate PUNTI.<br><br>
103 Chi vince l\'asta dovr&agrave; decidere il seme della carta scelta e inizier&agrave; la mano.<br>
104 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
105 Il vostro turno &egrave; sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
106 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
107 Dopo che &egrave; iniziata una partita per uscirne dovete chiedere agli altri giocatori di sbloccarla cliccando sul lucchetto. Se non si segue questa prassi, una volta usciti, non vi potrete sedere a nessun tavolo per '.floor(BAN_TIME/60).' minuti.
108 <dl>
109 <dt><b>Comandi della chat</b>
110 <dd><b>/nick <i>&lt;nuovo_nickname&gt;</i></b> - cambio di nickname
111 <dd><b>/tav <i>&lt;frase di invito&gt;</i></b> - invito per gli altri giocatori al tavolo dove si &egrave; seduti 
112 <dd><b>/st <i>&lt;stato&gt;</i></b> - cambia l\'icona associata al tuo user; <i>stato</i> pu&ograve; valere: \\"normale\\", \\"fuori\\", \\"pausa\\", \\"cibo\\", \\"cane\\", \\"lavoro\\", \\"presente\\" oppure \\"sigaretta\\"
113 <dd><b>/garante</b> - se si &egrave; autenticati permette di garantire per un utente fidato
114 </dl>
115 </div>
116 ';
117
118 //  
119
120 $G_room_about= '<br>
121 <div id=\\"header\\" class=\\"header\\">
122   <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
123   briscola chiamata in salsa ajax
124 </div>
125 <br><b>version '.$G_brisk_version.'</b><br><br>
126 Copyright 2006-2008 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>';
127
128
129 function xcape($s)
130 {
131   $from = array (   '\\',     '@',        '|' );
132   $to   = array ( '\\\\', '&#64;', '&brvbar;' );
133
134   return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
135 }
136
137 function xcapelt($s)
138 {
139   $from = array (   '\\',     '|' );
140   $to   = array ( '\\\\',   '\\|' );
141
142   return (str_replace($from, $to, $s));
143 }
144
145 class Card {
146   var $value; /* 0 - 39 card value */
147   var $stat;  /* 'bunch', 'hand', 'table', 'take' */
148   var $owner; /* (table position 0-4) */
149   // var $pos;   /* Pos in hand. */
150   var $x;     /* When played the X position on the table of the owner. */
151   var $y;     /* When played the Y position on the table of the owner. */
152
153   function Card($value, $stat, $owner)
154   {
155     $this->value = $value;
156     $this->stat  = $stat; // Card stat
157     $this->owner = $owner;
158   }
159
160   function assign($stat,$owner)
161   {
162     $this->stat  = $stat; // Card stat
163     $this->owner = $owner;
164   }
165
166   function setpos($pos)
167   {
168     $this->pos   = $pos;
169   }
170
171   function play($x,$y)
172   {
173     $this->stat = 'table'; // Card stat
174     $this->x = $x;
175     $this->y = $y;
176   }
177
178   function take($newown)
179   {
180     $this->stat = 'take'; // Card stat
181     $this->owner = $newown;
182   }
183 } // end class Card
184
185 class Table {
186   var $idx;
187   var $player;
188   var $player_n;
189   var $card;
190   var $mazzo;
191   var $gstart;
192   var $turn;
193   var $auth_only;
194
195   var $wag_own;
196   var $wag_com;
197   var $wag_tout;
198
199   var $asta_pla;
200   var $asta_pla_n;
201   var $asta_card;
202   var $asta_pnt;
203   
204   var $mult;
205   var $points;    // points array
206   var $points_n;  // number of row of points
207   var $total;
208
209   var $asta_win;
210   var $briscola;
211   var $friend;
212   
213   var $old_reason;
214   var $old_asta_pnt;
215   var $old_pnt;
216   var $old_win;
217   var $old_friend;
218
219   var $table_token;
220   var $table_start;
221
222   var $wakeup_time;
223
224   function Table() 
225   {
226   }
227   
228   function &create($idx) 
229   {
230     GLOBAL $G_false;
231
232     if (($thiz =& new Table()) == FALSE)
233       return ($G_false);
234
235     $thiz->idx       =   $idx;
236     $thiz->player    =   array();
237     $thiz->player_n  =   0;
238     $thiz->card      =   FALSE;
239     $thiz->asta_pla  =   array(); // TRUE: in auction, FALSE: out of the auction
240     $thiz->asta_pla_n=  -1;
241     $thiz->asta_card =  -1;
242     $thiz->asta_pnt  =  -1;
243     $thiz->mult      =   1;
244     
245     $thiz->auth_only =   FALSE;
246
247     $thiz->points    =   array( );
248     $thiz->points_n  =   0;
249     $thiz->total     =   array( 0, 0, 0, 0, 0);
250     $thiz->asta_win  =  -1;
251     $thiz->briscola  =  -1;
252     $thiz->friend    =  -1;
253     $thiz->turn      =   0;
254
255     $thiz->wag_own   =  NULL;
256     $thiz->wag_com   =  "";
257     $thiz->wag_tout   =  0;
258
259     $thiz->old_reason   = "";
260     $thiz->old_asta_pnt = -1;
261     $thiz->old_pnt      = -1;
262     $thiz->old_win      = -1;
263     $thiz->old_friend   = -1;
264
265     $thiz->table_token  = "";
266     $thiz->table_start  = 0;
267     
268     $thiz->wakeup_time = 0;
269
270     return ($thiz);
271   }
272
273   function &clone(&$from)
274   {
275     GLOBAL $G_false;
276     
277     if (($thiz =& new Table()) == FALSE)
278       return ($G_false);
279     
280     $thiz->idx = $from->idx;
281     $thiz->player = array();
282     for ($i = 0 ; $i < $from->player_n ; $i++)
283       $thiz->player[$i] = $from->player[$i];
284     $thiz->player_n = $from->player_n;
285     $thiz->card = $from->card;
286     $thiz->mazzo = $from->mazzo; // REVIEW
287     $thiz->gstart = $from->gstart;
288     $thiz->turn = $from->turn;
289
290     $thiz->auth_only =  $from->auth_only;
291
292     $thiz->wag_own   =  $from->wag_own;
293     $thiz->wag_com   =  $from->wag_com;
294     $thiz->wag_tout  =  $from->wag_taut;
295
296     $thiz->asta_pla = $from->asta_pla;
297     $thiz->asta_pla_n = $from->asta_pla_n;
298     $thiz->asta_card = $from->asta_card;
299     $thiz->asta_pnt = $from->asta_pnt;
300     
301     $thiz->mult = $from->mult;
302     $thiz->points = $from->points;
303     $thiz->points_n = $from->points_n;
304     $thiz->total = $from->total;
305     
306     $thiz->asta_win = $from->asta_win;
307     $thiz->briscola = $from->briscola;
308     $thiz->friend = $from->friend;
309     
310     $thiz->old_reason = $from->old_reason;
311     $thiz->old_asta_pnt = $from->old_asta_pnt;
312     $thiz->old_pnt = $from->old_pnt;
313     $thiz->old_win = $from->old_win;
314     $thiz->old_friend = $from->old_friend;
315
316     $thiz->table_token  = $from->table_token;
317     $thiz->table_start  = $from->table_start;
318
319     $thiz->wakeup_time = $from->wakeup_time;
320
321     return ($thiz);
322   }
323   
324   function &spawn(&$from)
325   {
326     GLOBAL $G_false;
327     
328     if (($thiz =& new Table()) == FALSE)
329       return ($G_false);
330     
331     $thiz->idx = $from->idx;
332     $thiz->player_n = $from->player_n;
333     $thiz->card = &$thiz->bunch_create();
334     $thiz->mazzo = $from->mazzo;
335     $thiz->gstart = $from->gstart;
336     $thiz->turn = $from->turn;
337
338     $thiz->auth_only =  $from->auth_only;
339
340     $thiz->wag_own = $from->wag_own;
341     $thiz->wag_com = $from->wag_com;
342     $thiz->wag_tout  =  $from->wag_taut;
343
344     $thiz->asta_pla = $from->asta_pla;
345     $thiz->asta_pla_n = $from->asta_pla_n;
346     $thiz->asta_card = $from->asta_card;
347     $thiz->asta_pnt = $from->asta_pnt;
348     
349     $thiz->mult = $from->mult;
350     $thiz->points = $from->points;
351     $thiz->points_n = $from->points_n;
352     $thiz->total = $from->total;
353     
354     $thiz->asta_win = $from->asta_win;
355     $thiz->briscola = $from->briscola;
356     $thiz->friend = $from->friend;
357     
358     $thiz->old_reason = $from->old_reason;
359     $thiz->old_asta_pnt = $from->old_asta_pnt;
360     $thiz->old_pnt = $from->old_pnt;
361     $thiz->old_win = $from->old_win;
362     $thiz->old_friend = $from->old_friend;
363
364     // players are rearranged in an dedicated array
365     $thiz->player = array();
366     for ($i = 0 ; $i < $from->player_n ; $i++)
367       $thiz->player[$i] = $i;
368
369     $thiz->table_token  = $from->table_token;
370     $thiz->table_start  = $from->table_start;
371
372     $thiz->wakeup_time = $from->wakeup_time;
373
374     return ($thiz);
375   }
376   
377   function &bunch_create()
378   {
379     $ret = array();
380
381     for ($i = 0 ; $i < 40 ; $i++) {
382       $ret[$i] =& new Card($i, 'bunch', 'no_owner');
383     }
384
385     $oret = &$ret;
386     return ($oret);
387   }
388
389   function wag_set(&$user, $mesg)
390   {
391     log_main("WAG_SET");
392
393     $this->wag_own  = &$user;
394     $this->wag_com  =  $mesg;
395     $this->wag_tout =  0;
396   }
397
398   function wag_reset($timeout)
399   {
400     log_main("WAG_RESET");
401
402     unset($this->wag_own);
403     $this->wag_own = NULL;
404     $this->wag_com  = "";
405     $this->wag_tout = $timeout;
406   }
407
408   function bunch_make()
409   {
410     $ct = array(0,0,0,0,0);
411     
412     mt_srand(make_seed());
413     
414     for ($i = 39 ; $i >= 0 ; $i--) 
415       $rest[$i] = $i;
416
417     for ($i = 39 ; $i >= 0 ; $i--) {
418       $rn = rand(0, $i);
419       
420       if ($rn == 0)
421         log_main("RND ZERO");
422       
423       $id = $rest[$rn];
424
425       $owner = $i % 5;
426       $this->card[$id]->assign('hand', $owner);
427
428       $rest[$rn] = $rest[$i];
429       // $pubbpos[$rn2] = $pubbpos[$i];
430     }
431   }
432
433   function init(&$userarr)
434   {
435     $this->mazzo    = rand(0,PLAYERS_N-1);
436     $this->points_n = 0;
437     $this->mult     = 1;
438     $this->old_win  =-1;
439     $this->old_reason = "";
440     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
441       $this->total[$i] = 0;
442       $user_cur = &$userarr[$this->player[$i]];
443       $user_cur->exitislock = TRUE;
444     }
445
446     log_main("table::init: ci siamo");
447   }
448
449   function game_init(&$userarr)
450   {
451     log_rd2("GSTART 4");
452
453     $this->gstart = ($this->mazzo+1) % PLAYERS_N;
454     $this->bunch_make();
455     
456     
457     $this->asta_pla_n = PLAYERS_N;
458     $this->asta_card = -1;
459     $this->asta_pnt  = 60;
460     $this->asta_win  = -1;
461     $this->briscola  = -1;
462     $this->friend    = -1;
463     $this->turn      =  0;
464     
465     for ($i = 0 ; $i < PLAYERS_N ; $i++) {
466       $this->asta_pla[$i] = TRUE;
467       $user_cur = &$userarr[$this->player[$i]];
468       $user_cur->subst = 'asta';
469       $user_cur->asta_card = -2;
470       $user_cur->asta_pnt  = -1;
471       $user_cur->handpt = $this->hand_points($i);
472       // SEE function calculate_points(&$table)
473     }
474   }
475
476   function game_next()
477   {
478     $this->mazzo  = ($this->mazzo + 1) % PLAYERS_N;
479   }
480
481   function getPlayer($idx)
482   {
483     return ($this->player[$idx]);
484   }
485
486   function setPlayer($idx, $player)
487   {
488     $this->player[$idx] = $player;
489   }
490
491   function user_add($idx)
492   {
493     $this->player[$this->player_n] = $idx;
494     $this->player_n++;
495     
496     return ($this->player_n - 1);
497   }
498   
499   function user_rem(&$room, &$user)
500   {
501     $tabpos = $user->table_pos;
502     
503     /* verifico la consistenza dei dati */
504     if ($room->user[$this->player[$tabpos]] == $user) {
505       
506       /* aggiorna l'array dei giocatori al tavolo. */
507       for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
508         $this->player[$i] = $this->player[$i+1];
509         $user_cur = &$room->user[$this->player[$i]];
510         $user_cur->table_pos = $i;
511       }
512       $this->player_n--;
513     }
514     else {
515       log_main("INCONSISTENCY ON TABLE.");
516     }
517   }
518
519   function hand_points($idx)
520   {
521     GLOBAL $G_all_points; 
522     
523     $tot = 0;
524     
525     for ($i = 0 ; $i < 40 ; $i++) {
526       if ($this->card[$i]->owner != $idx)
527         continue;
528
529       $ctt = $this->card[$i]->value % 10;
530       $tot += $G_all_points[$ctt];
531     }
532
533     return ($tot);
534   }
535
536   function exitlock_show(&$userarr, $table_pos)
537   {
538     $ct = $this->exitlock_calc(&$userarr, $table_pos);
539
540     $ret = sprintf('exitlock_show(%d, %s);', $ct, 
541                    ($userarr[$this->player[$table_pos]]->exitislock ? 'true' : 'false'));
542     return ($ret);
543   }
544
545   function exitlock_calc(&$userarr, $table_pos)
546   {
547     $ct = 0;
548
549     for ($i = 0 , $ct = 0 ; $i < PLAYERS_N ; $i++) {    
550       if ($userarr[$this->player[$i]]->exitislock == FALSE)
551         $ct++;
552     }
553
554     return ($ct);
555   }
556
557
558
559
560   //      $ret .= table_act_content(($user->subst == 'standup'), $this->table[$i]->player_n, $i, $user->table, 
561   //                              ($this->table[$i]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
562
563   // function act_content($isstanding, $sitted, $table, $cur_table, $allowed)
564   function act_content(&$user)
565   {
566     $ret = "";
567     $isstanding = ($user->subst == 'standup');
568     $sitted = $this->player_n;
569     $table = $this->idx;
570     $cur_table = $user->table;
571     $allowed = TRUE;
572
573     if ($isstanding) {
574       if ($sitted < PLAYERS_N) {
575         if ($this->auth_only) {
576           if ($user->flags & USER_FLAG_AUTH) 
577             $act = "sitreser";
578           else
579             $act = 'reserved';
580         }
581         else {
582           $act = 'sit';
583         }
584       }
585       else {
586         $act = 'none';
587       }
588     }
589     else {
590       if ($table == $cur_table)
591         $act = 'wake';
592       else
593         $act = 'none';
594     }
595     
596     if ($act != '')
597       $ret = sprintf('j_tab_act_cont(%d, \'%s\');', $table, $act);
598     
599     return ($ret);
600   }
601
602
603 } // end class Table
604   
605
606 // User flags
607 define(USER_FLAG_AUTH, 0x02);
608
609 //   user status
610 define(USER_FLAG_S_NORM,  0x000); // done
611 define(USER_FLAG_S_PAU,   0x100); // done
612 define(USER_FLAG_S_OUT,   0x200); // done
613 define(USER_FLAG_S_DOG,   0x300); // done
614 define(USER_FLAG_S_EAT,   0x400); // done
615 define(USER_FLAG_S_WRK,   0x500); // done
616 define(USER_FLAG_S_SMK,   0x600); // done
617 define(USER_FLAG_S_EYE,   0x700); // done
618
619 define(USER_FLAG_S_ALL,   0xf00); // done
620
621 class User {
622   var $name;       // name of the user
623   var $sess;       // session of the user
624   var $ip;         // ip of the user
625   var $lacc;       // last access (for the cleanup)
626   var $laccwr;     // last access (for the cleanup)
627   var $bantime;    // timeout to temporary ban
628   var $stat;       // status (outdoor, room, table, game, ...)
629   var $subst;      // substatus for each status   
630   var $step;       // step of the current status
631   var $trans_step; // step to enable transition between pages (disable == -1)
632   var $comm;       // commands array
633   var $asta_card;  // 
634   var $asta_pnt;   //
635   var $handpt;     // Total card points at the beginning of the current hand.
636   var $exitislock; // Player can exit from the table ?
637   var $table;      // id of the current table (if in table state)
638   var $table_pos;  // idx on the table
639   var $table_token;// token that identify a game on a table
640   var $flags;      // Bitfield with: AUTHENTICATE: 0x02 
641   var $the_end;    // Flag to change the end of the session
642
643   var $chat_lst;      // Last chat line
644   var $chattime;      // Array of chat times
645   var $chat_cur;      // Current chat line number
646   var $chat_ban;      // Time for ban chat
647   var $chat_dlt;      // Delta t for ban
648   function User() {
649   }
650
651   function &create($name, $sess, $stat = "", $subst = "", $table = -1, $ip="0.0.0.0") {
652     GLOBAL $G_false;
653
654     if (($thiz =& new User()) == FALSE)
655       return ($G_false);
656
657     $thiz->name  = $name;
658     $thiz->sess  = $sess;
659     $thiz->ip    = $ip;
660     $thiz->lacc   = time();
661     $thiz->laccwr = time();
662     $thiz->bantime = 0;
663     $thiz->stat  = $stat;
664     $thiz->subst  = $subst;
665     $thiz->step  = 1;
666     $thiz->trans_step  = -1;
667     $thiz->comm  = array();
668     $thiz->asta_card = -2;
669     $thiz->asta_pnt  = -1;
670     $thiz->handpt = -1;
671     $thiz->exitislock = TRUE;
672
673     $thiz->flags = 0x00;
674     
675     $thiz->chattime = array_fill(0, CHAT_N, 0);
676     $thiz->chat_cur = 0;
677     $thiz->chat_lst = "";
678     $thiz->chat_ban = 0;
679     $thiz->chat_dlt = 0;
680
681     $thiz->table = $table;
682     $thiz->table_pos = -1;
683     $thiz->table_token = "";
684
685     return ($thiz);
686   }
687
688   function &clone(&$from)
689   {
690     GLOBAL $G_false;
691     
692     if (($thiz =& new User()) == FALSE)
693       return ($G_false);
694     
695     $thiz->name       = $from->name;
696     $thiz->sess       = $from->sess;
697     $thiz->ip         = $from->ip;
698     $thiz->lacc       = $from->lacc;
699     $thiz->laccwr     = $from->laccwr;
700     $thiz->bantime    = $from->bantime;
701     $thiz->stat       = $from->stat;
702     $thiz->subst      = $from->subst;
703     $thiz->step       = $from->step;
704     $thiz->trans_step = $from->trans_step;
705     $thiz->comm       = array();
706
707     $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N)); 
708     for ($i = $i_start ; $i < $from->step ; $i++) {
709       $ii = $i % COMM_N;
710       $thiz->comm[$ii] = $from->comm[$ii];
711     }
712     $thiz->asta_card  = $from->asta_card;
713     $thiz->asta_pnt   = $from->asta_pnt;
714     $thiz->handpt     = $from->handpt;
715     $thiz->exitislock = $from->exitislock;
716
717     $thiz->flags = $from->flags;
718
719     $thiz->chattime = array();
720     for ($i = 0 ; $i < CHAT_N ; $i++)
721       $thiz->chattime[$i] = $from->chattime[$i];
722     $thiz->chat_cur = $from->chat_cur;
723     $thiz->chat_lst = $from->chat_lst;
724     $thiz->chat_ban = $from->chat_ban;
725     $thiz->chat_dlt = $from->chat_dlt;
726
727     $thiz->table      = $from->table;
728     $thiz->table_pos  = $from->table_pos;
729     $thiz->table_token = $from->table_token;
730     $thiz->the_end    = $from->the_end;
731
732     return ($thiz);
733   }
734   
735   function &spawn(&$from, $table, $table_pos)
736   {
737     GLOBAL $G_false;
738     
739     if (($thiz =& new User()) == FALSE)
740       return ($G_false);
741     
742     $thiz->name       = $from->name;
743     $thiz->sess       = $from->sess;
744     $thiz->ip         = $from->ip;
745     $thiz->lacc       = $from->lacc;
746     $thiz->laccwr     = $from->laccwr;
747     $thiz->bantime    = $from->bantime;
748     $thiz->stat       = $from->stat;
749     $thiz->subst      = $from->subst;
750     $thiz->step       = $from->step;
751     $thiz->trans_step = $from->trans_step;
752     $thiz->comm       = array();
753
754     /*
755     $i_start = (1 > ($from->step - COMM_N) ? 1 : ($from->step - COMM_N)); 
756     for ($i = $i_start ; $i < $from->step ; $i++) {
757       log_wr("TRY PUSH:".$i);
758       $ii = $i % COMM_N;
759       $thiz->comm[$ii]   = $from->comm[$ii];
760     }
761     */
762     $thiz->asta_card  = $from->asta_card;
763     $thiz->asta_pnt   = $from->asta_pnt;
764     $thiz->handpt     = $from->handpt;
765     $thiz->exitislock = $from->exitislock;
766     $thiz->the_end    = $from->the_end;
767
768     $thiz->flags      = $from->flags;
769
770     $thiz->chattime   = array_fill(0, CHAT_N, 0);
771     $thiz->chat_cur   = 0;
772     $thiz->chat_lst   = "";
773     $thiz->chat_ban   = 0;
774     $thiz->chat_dlt   = 0;
775
776
777     $thiz->table      = $table;
778     $thiz->table_pos  = $table_pos;
779     $thiz->table_token = $from->table_token;
780
781     return ($thiz);
782   }
783   
784   function stat_set($stat) {
785     log_main("sess: [".$this->sess. "] NEW STAT: [".$stat."]"); 
786     $this->stat = "$stat";
787     
788     /*
789     if (validate_sess($this->sess)) {
790       if (file_exists(PROXY_PATH) == FALSE)
791         mkdir(PROXY_PATH);
792       $fp = fopen(PROXY_PATH."/".$this->sess.".stat", 'w');
793       fwrite($fp, sprintf("%s\n",$this->stat));
794       fclose($fp);
795     }
796     */
797   }
798
799   function step_set($step) {
800     $this->step = $step;
801     
802     do {
803       if (validate_sess($this->sess) == FALSE)
804         break;
805       if (file_exists(PROXY_PATH) == FALSE)
806         mkdir(PROXY_PATH);
807       if (($fp = @fopen(PROXY_PATH."/".$this->sess.".step", 'w')) == FALSE)
808         break;
809       fwrite($fp, pack("l",$this->step), 4);
810       fclose($fp);
811
812       return (TRUE);
813     } while (0);
814
815     return (FALSE);
816   }
817
818   function step_inc($delta = 1) {
819     $this->step += $delta;
820     
821     if (validate_sess($this->sess)) {
822       if (file_exists(PROXY_PATH) == FALSE)
823         mkdir(PROXY_PATH);
824       $fp = fopen(PROXY_PATH."/".$this->sess.".step", 'w');
825       fwrite($fp, pack("l",$this->step), 4);
826       fclose($fp);
827
828       return (TRUE);
829     }
830     
831     return (FALSE);
832   }
833
834   function reset() {
835     $curtime = time();
836     log_legal($curtime, $this, "STAT:LOGOUT", '');
837
838     $tmp_sess = $this->sess;
839     $this->sess = "";
840     step_unproxy($tmp_sess);
841     $this->name = "";  // OK here
842     while (array_pop($this->comm) != NULL);
843     $this->step = 0;
844     $this->chattime = array_fill(0, CHAT_N, 0);
845     $this->chat_cur = 0;
846     $this->chat_lst = "";
847     $this->chat_ban = 0;
848     $this->chat_dlt = 0;
849     $this->the_end = FALSE;
850   }
851 } // end class User
852
853
854 function step_get($sess) {
855   $fp = FALSE;
856   do {
857     if (validate_sess($sess) == FALSE)
858       break;
859
860     if (file_exists(PROXY_PATH) == FALSE)
861       mkdir(PROXY_PATH);
862     if (($fp = @fopen(PROXY_PATH."/".$sess.".step", 'rb')) == FALSE)
863       break;
864     if (($s = fread($fp, 4)) == FALSE)
865       break;
866     if (strlen($s) != 4)
867       break;
868     $arr = unpack('l', $s);
869     fclose($fp);
870
871     // log_rd2("A0: ".$arr[0]."  A1: ".$arr[1]);
872     return ($arr[1]);
873   } while (0);
874
875   if ($fp != FALSE)
876     fclose($fp);
877
878   log_rd2("STEP_GET: return false ");
879
880   return (FALSE);
881 }
882
883 function step_unproxy($sess) {
884   log_rd2("UNPROXY: ".PROXY_PATH."/".$sess.".step");
885   if (file_exists(PROXY_PATH) == FALSE)
886     mkdir(PROXY_PATH);
887   @unlink(PROXY_PATH."/".$sess.".step");
888 }
889
890
891 class Room {
892   var $user;
893   var $table;
894   var $comm; // commands for many people
895   var $step; // current step of the comm array
896   var $garbage_timeout;
897   var $shm_sz;
898
899   function Room () {
900     $this->user = array();
901     $this->table = array();
902
903     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
904       $this->user[$i] =& User::create("", "");
905     }
906
907     for ($i = 0 ; $i < TABLES_N ; $i++) {
908       $this->table[$i] =& Table::create($i);
909       if ($i < 12) {
910         $row = ( (((int)($i / 4)) % 2) == 0 );
911         $col = ($i % 2 == 0);
912         $this->table[$i]->auth_only = (($row && $col) || (!$row && !$col));
913       }
914       else {
915         $this->table[$i]->auth_only = FALSE;
916       }
917     }
918     $this->garbage_timeout = 0;
919   }
920
921   function garbage_manager($force)
922   {
923     $ismod = FALSE;
924
925     log_rd2("garbage_manager START");
926
927     /* Garbage collector degli utenti in timeout */
928     $curtime = time();
929     if ($force || $this->garbage_timeout < $curtime) {
930       
931       // Before all align times with table timeout
932       for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
933         $table_cur =& $this->table[$table_idx];
934         // if the table is complete and exists its shared mem we get the info about users lacc
935
936         if ($table_cur->player_n == PLAYERS_N) {
937           log_main("PLAYERS == N TABLE ".$table_idx);
938           
939           if (($sem = Briskin5::lock_data($table_idx)) != FALSE) { 
940             log_main("bin5 lock data success");
941             
942             $no_recovery = FALSE;
943             if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
944               if ($table_cur->table_token != $bri->table_token) {
945                 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token."  Table: ".$bri->table_token);
946                 log_main("ERROR: not matching table_start. Room: ".$table_cur->table_start."  Table: ".$bri->table_start);
947                 $no_recovery = TRUE;
948                 $bri = FALSE;
949               }
950             }
951             
952             if ($bri != FALSE) {
953               //
954               //  SPAWN: JOIN
955               //
956               log_main("garbage_manager: bri loaded successfully.");
957               $bri->garbage_manager(TRUE);
958               
959               $bri_table = &$bri->table[0];
960
961               // is the end of the table
962               if ($bri->the_end == TRUE) {
963                 /*
964                  *  DESTROY OF FINISHED TABLE && MOVE PLAYER TO ROOM AGAIN
965                  */
966                 log_main("garbage_manager: INSIDE THE END.");
967
968                 $plist = "$table_cur->table_token|$user_cur->table|$table_cur->player_n";
969                 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
970                   $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
971                 }
972
973                 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
974                   // stat must be "table" by definition
975                   $user_cur =& $this->user[$table_cur->player[$i]];
976                   $bri_user =& $bri->user[$i];
977                   
978                   $user_cur->subst      = $bri_user->subst;
979                   $user_cur->step       = $bri_user->step;
980                   $user_cur->lacc       = $bri_user->lacc;
981                   $user_cur->laccwr     = $bri_user->lacc;
982                   $user_cur->bantime    = $bri_user->bantime;
983                 }
984
985                 log_legal($curtime, $user_cur, "STAT:DESTROY_GAME", $plist);
986
987                 $this->room_join_wakeup(&$user_cur, FALSE, 0); 
988                 $table_cur->table_token = "";
989                 $table_cur->wakeup_time = $curtime + WAKEUP_TIME;
990                 Briskin5::destroy_data($table_idx);
991               }
992               else {
993                 log_main("gm:: save_data");
994
995                 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
996                   $this->user[$table_cur->player[$i]]->lacc = $bri->user[$i]->lacc;
997                 }
998               
999                 Briskin5::save_data(&$bri);
1000               }
1001             } // else if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
1002             else if ($no_recovery == FALSE) {
1003               log_crit("ERROR: table ".$table_idx." unrecoverable join");
1004
1005               for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
1006                 $user_cur = &$this->user[$table_cur->player[$i]];
1007                 $user_cur->subst = "shutdowner";
1008                 $user_cur->step_inc();
1009                 
1010                 $ret = sprintf('stat = "%s"; subst = "%s";',  $user_cur->stat, $user_cur->subst);
1011                 $ret .= "gst.st = ".($user_cur->step+1)."; ";
1012                 $ret .= show_notify("<br>I dati del tavolo n&deg; ".$user_cur->table." sono inconsistenti, verranno resettati.<br><br>Torni in piedi.<br><br>", 2000, "Chiudi.", 400, 110);
1013                 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1014                 $user_cur->step_inc();
1015               }
1016
1017               $plist = "$table_cur->table_token|$user_cur->table|$table_cur->player_n";
1018               for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
1019                 $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
1020               }
1021               log_legal($curtime, $user_cur, "STAT:DESTROY_GAME(RECOVERY)", $plist);
1022               
1023               $this->room_join_wakeup(&$user_cur, TRUE, -2); 
1024               $table_cur->table_token = "";
1025             }
1026
1027             Briskin5::unlock_data($sem);
1028           } // bri::lock_data
1029         } //  if ($table_cur->player_n == PLAYERS_N) {
1030       } //  for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1031     
1032       log_rd2("out new loop.");
1033                 
1034       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1035         $user_cur = &$this->user[$i];
1036         
1037         log_rd2("User: ".$user_cur->name."  stat: ".$user_cur->stat."  subst: ".$user_cur->subst);
1038           
1039         if ($user_cur->sess == "") 
1040           continue;
1041         
1042         if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) {
1043           // Auto logout dell'utente
1044           log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime ".$curtime);
1045           
1046           if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
1047             log_auth($user_cur->sess, "Autologout session.");
1048             
1049             $user_cur->reset();
1050             
1051             log_rd2("AUTO LOGOUT.");
1052             if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
1053               $this->room_wakeup(&$user_cur);
1054             else if ($user_cur->subst == 'standup')
1055               $this->room_outstandup(&$user_cur);
1056             else
1057               log_rd2("LOGOUT FROM WHAT ???");
1058           }
1059         }
1060
1061         if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
1062           if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
1063             $this->room_wakeup(&$user_cur);
1064             $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1065             /* MLANG: <br>Sei stato inattivo per ".(EXPIRE_TIME_SMAMMA/60.0)." minuti. <br><br>Quindi ritorni tra i <b>Giocatori in piedi</b>.", "torna ai tavoli" */
1066             $user_cur->comm[$user_cur->step % COMM_N] .=  show_notify("<br>Sei stato inattivo per ".(EXPIRE_TIME_SMAMMA/60.0)." minuti. <br><br>Quindi ritorni tra i <b>Giocatori in piedi</b>.", 0, "torna ai tavoli", 400, 100);
1067             $user_cur->step_inc();
1068           }
1069         }
1070       }
1071       log_rd2("GARBAGE UPDATED!");
1072       
1073       $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
1074       $ismod = TRUE;
1075     }
1076
1077     return ($ismod);
1078   }
1079
1080   function show_room($user_step, &$user)
1081   {
1082     log_main("show_room: username: ".$user->name);
1083     
1084     
1085     $ret = sprintf('gst.st = %d; ',  $user_step);
1086     if ($user->subst == 'standup')
1087       $ret .= "tra.show(); ";
1088     else
1089       $ret .= "tra.hide(); ";
1090
1091     $ret .= sprintf('stat = "%s";',  $user->stat);
1092     
1093     $ret .= root_wellcome($user);
1094     $ret .= sprintf('subst = "%s";', $user->subst);
1095     $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
1096     $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
1097     $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>";', $itin, xcape($user->name), $itou);
1098     for ($i = 0 ; $i < TABLES_N ; $i++) {
1099       $ret .= $this->table_content($user, $i);
1100       // $ret .= table_act_content(($user->subst == 'standup'), $this->table[$i]->player_n, $i, $user->table, 
1101       //                          ($this->table[$i]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
1102       $ret .=  $this->table[$i]->act_content($user);
1103       if ($this->table[$i]->wag_own != NULL) 
1104         $ret .= sprintf('tra.add(%d, "%s: %s"); ', $i,  $this->table[$i]->wag_own->name, $this->table[$i]->wag_com);
1105       else 
1106         $ret .= sprintf('tra.rem(%d); ', $i);
1107     }
1108     $ret .= $this->standup_content($user);
1109     
1110     return ($ret);
1111   }
1112   
1113
1114   function room_wakeup(&$user)
1115   {
1116     $table_idx = $user->table;
1117     $table = &$this->table[$table_idx];
1118
1119     log_main("WAKEUP: begin function table:".$table_idx."  stat: ".$user->stat."  subst: ".$user->subst);
1120
1121     $curtime = time();
1122
1123     $from_table = ($user->stat == "table");
1124     if ($from_table) {
1125       log_main("WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1126
1127       for ($i = 0 ; $i < $table->player_n ; $i++) {
1128         $user_cur = &$this->user[$table->player[$i]];
1129         log_main("PREIMPOST: INLOOP name: ".$user_cur->name);
1130
1131         if ($user_cur != $user) {
1132           $user_cur->stat_set("room");
1133           $user_cur->subst = "sitdown";
1134           $user_cur->laccwr = $curtime;
1135         }
1136         else if ($user->sess != "") {
1137           $user_cur->stat_set("room");
1138           $user_cur->subst = "standup";
1139           $user_cur->laccwr = $curtime;
1140           $user_cur->table = -1;
1141         }
1142       }
1143     }
1144     else {
1145       $user->stat_set("room");
1146       $user->subst = "standup";
1147       $user->laccwr = $curtime;
1148     }
1149     
1150     $remove_wagon = false;
1151     if($table->wag_own == $user) {
1152       $remove_wagon = true;
1153       $table->wag_reset($curtime);
1154       
1155     }
1156     
1157
1158     /* aggiorna l'array dei giocatori al tavolo. */
1159     $table->user_rem(&$this, &$user);
1160
1161     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1162       $user_cur = &$this->user[$i];
1163       if ($user_cur->sess == '' || $user_cur->stat != 'room')
1164         continue;
1165       
1166       log_main("VALORI: name: ".$user_cur->name."from_table: ".$from_table."  tab: ".$user_cur->table." taix: ".$table_idx."  ucur: ".$user_cur."  us: ".$user);
1167
1168       $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
1169       if ($from_table && ($user_cur->table == $table_idx || $user_cur == $user)) {
1170         $ret .= 'gst.st_loc++; the_end=true; window.onunload = null; window.onbeforeunload = null; document.location.assign("index.php");|';
1171         // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
1172         log_main("DOCUMENT.index.php: from table");
1173       }
1174       else if ($user_cur->stat == "room") {
1175         log_main("DOCUMENT.index.php: from table");
1176
1177         $ret .= $this->table_content($user_cur, $table_idx);
1178         $ret .= $this->standup_content($user_cur);
1179         
1180         // $ret .= table_act_content(FALSE, 0, $table_idx, $user->table, FALSE);
1181         $ret .= $table->act_content($user);
1182
1183         if ($user_cur == $user) {
1184           // set the new status 
1185           $ret .=  'subst = "standup"; tra.show(); ';
1186           // clean the action buttons in other tables
1187           for ($e = 0 ; $e < TABLES_N ; $e++) {
1188             if ($this->table[$e]->player_n < PLAYERS_N) {
1189               // $ret .= table_act_content(TRUE, 0, $e, $user->table, 
1190               //                           ($this->table[$e]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
1191               $ret .= $this->table[$e]->act_content($user);
1192             }
1193           }
1194         }
1195         else {
1196           // $ret .= table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table,
1197           //                           ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1198           $ret .= $table->act_content($user_cur);
1199         }
1200       }
1201       log_wr("ROOM_WAKEUP: ".$ret);
1202       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1203       $user_cur->step_inc();
1204     }
1205   }
1206
1207   function room_join_wakeup(&$user, $update_lacc = FALSE, $trans_delta)
1208   {
1209     $table_idx = $user->table;
1210     $table = &$this->table[$table_idx];
1211     
1212     log_main("JOIN_WAKEUP: begin function table:".$table_idx."  stat: ".$user->stat."  subst: ".$user->subst);
1213
1214     $curtime = time();
1215     $user_wup = array();
1216     $user_wup_n = 0;
1217     $user_tab = array();
1218     $user_tab_n = 0;
1219     log_main("JOIN WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1220     
1221     for ($i = 0 ; $i < $table->player_n ; $i++) {
1222       $user_cur = &$this->user[$table->player[$i]];
1223       log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1224       if ($user_cur->sess != "") {
1225         if ($update_lacc == TRUE) {
1226           $user_cur->laccwr = $curtime;
1227         }
1228         log_main("cur: ".$user_cur->name."  subst: ".$user_cur->subst);
1229         if ($user_cur->subst == "shutdowned") {
1230           $user_cur->stat_set("room");
1231           $user_cur->subst = "sitdown";
1232         }
1233         else if ($user_cur->subst == "shutdowner") {
1234           $user_cur->stat_set("room");
1235           $user_cur->subst = "standup";
1236           $user_cur->table = -1;
1237           $user_wup[$user_wup_n++] = &$user_cur;
1238           
1239           $remove_wagon = false;
1240           if($table->wag_own == $user_cur) {
1241             $remove_wagon = true;
1242             $table->wag_reset($curtime);
1243           }
1244         }
1245         $user_tab[$user_tab_n++] = &$user_cur;
1246       }
1247     }
1248
1249     for ($wup_idx = 0 ; $wup_idx < $user_wup_n  ; $wup_idx++)
1250       $table->user_rem(&$this, &$user_wup[$wup_idx]);
1251
1252     /* aggiorna l'array dei giocatori al tavolo. */
1253
1254     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1255       log_main("START LOOP");
1256       $user_cur = &$this->user[$i];
1257       if ($user_cur->sess == '' || $user_cur->stat != 'room') {
1258         log_main("name: ".$user_cur->name."skip   subst: ".$user_cur->subst);
1259         continue;
1260       }
1261
1262       log_main("___");
1263       log_main("VALORI name: ".$user_cur->name."  tab: ".$user_cur->table." taix: ".$table_idx."  ucur: ".$user_cur."  us: ".$user);
1264
1265       $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
1266       if ($user_cur->stat == "room") {
1267         log_main("DOCUMENT.index.php from table");
1268
1269         $ret .= $this->table_content($user_cur, $table_idx);
1270         $ret .= $this->standup_content($user_cur);
1271         
1272         // $ret .= table_act_content(FALSE, 0, $table_idx, $user_cur->table,
1273         //                           ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1274         $ret .= $table->act_content($user_cur);
1275
1276
1277         for ($tab_idx = 0 ; $tab_idx < $user_tab_n  ; $tab_idx++)
1278           if ($user_cur == $user_tab[$tab_idx]) 
1279             break;
1280
1281         // for users that wakeup the room will be reconstructed by index_rd.php
1282         if ($tab_idx < $user_tab_n) {
1283           log_main("PRE show_room username: ".$user_cur->name."  STEP: ".$user_cur->step);
1284
1285 //        ARRAY_POP DISABLED
1286 //        if ($trans_delta == 0)
1287 //          while (array_pop($user_cur->comm) != NULL);
1288
1289           $user_cur->trans_step = $user_cur->step + 1 + $trans_delta;
1290           $user_cur->comm[$user_cur->step % COMM_N] = "";
1291           $user_cur->step_inc();
1292           $user_cur->comm[$user_cur->step % COMM_N] = $this->show_room(($user_cur->step + 1), &$user_cur);
1293           $user_cur->step_inc();
1294           log_main("POST show_room username: ".$user_cur->name."  STEP: ".$user_cur->step);
1295
1296           continue;
1297         }
1298         log_main("JOIN_WAKEUP wup_idx ".$wup_idx."  wup_n ".$user_wup_n);
1299
1300         log_main("JOIN_WAKEUP more");
1301         // $ret .= table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table,
1302         //                           ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1303         $ret .= $table->act_content($user_cur);
1304
1305         log_main("JOIN_WAKEUP end more");
1306       }
1307       log_wr("ROOM_JOIN_WAKEUP: ".$ret);
1308       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1309       $user_cur->step_inc();
1310     }
1311   }
1312
1313   function room_outstandup(&$user)
1314   {
1315     $this->room_sitdown(&$user, -1);
1316   }
1317   
1318   function table_update(&$user)
1319   {
1320     log_main("table_update: pre - USER: ".$user->name);
1321
1322     $table_idx = $user->table;
1323
1324     if ($table_idx > -1) 
1325       $table = &$this->table[$table_idx];
1326     
1327     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1328       $ret = "";
1329       $user_cur = &$this->user[$i];
1330       if ($user_cur->sess == '' || $user_cur->stat != 'room')
1331       continue;
1332       
1333       $ret = "gst.st = ".($user_cur->step+1)."; ";
1334       if ($table_idx > -1)
1335         $ret .= $this->table_content($user_cur, $table_idx);
1336       
1337       if ($user_cur == $user) {
1338         $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
1339         $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
1340         $ret .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>: ";', $itin, xcape($user->name), $itou);
1341       }
1342       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1343       $user_cur->step_inc();
1344     }
1345
1346     log_main("table_update: post");
1347   }
1348
1349   function room_sitdown(&$user, $table_idx)
1350   {
1351     log_main("room_sitdown ".($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
1352
1353     $train_app = "";
1354
1355     if ($table_idx > -1 && $table_idx < TABLES_N) { 
1356       $table = &$this->table[$table_idx];
1357
1358       // wagon shutdown 
1359       if ($table->wag_own != NULL && $table->player_n == PLAYERS_N) {        
1360         for ($i = 0 ; $i < TABLES_N ; $i++) {
1361           $user_cur =& $this->user[$table->player[$i]];
1362           if ($user_cur == $table->wag_own) {
1363             $train_app = sprintf("tra.rem(%d); ", $table_idx); 
1364             $table->wag_reset(time());
1365             break;
1366           }
1367         }
1368       }
1369     }
1370
1371     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1372       $ret = "";
1373       $user_cur = &$this->user[$i];
1374       if ($user_cur->sess == '' || $user_cur->stat != 'room')
1375       continue;
1376       
1377       $ret = "gst.st = ".($user_cur->step+1)."; ".$train_app;
1378       if ($table_idx > -1)
1379       $ret .= $this->table_content($user_cur, $table_idx);
1380       $ret .= $this->standup_content($user_cur);
1381       
1382       if ($user_cur == $user) {
1383         $ret .=  'subst = "sitdown"; tra.hide(); ';
1384         // clean the action buttons in other tables
1385         for ($e = 0 ; $e < TABLES_N ; $e++) {
1386           // $ret .= table_act_content(FALSE, 0, $e, $user_cur->table, FALSE);
1387           $ret .= $this->table[$e]->act_content($user_cur);
1388         }
1389       }
1390       else if ($table_idx > -1) {
1391         if ($table->player_n == PLAYERS_N) {
1392           // $ret .= table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table,
1393           ///                      ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1394           $ret .= $table->act_content($user_cur);
1395         }
1396       }
1397       $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1398       $user_cur->step_inc();
1399     }
1400   }
1401
1402   function chatt_send(&$user, $mesg)
1403   {
1404     GLOBAL $G_alarm_passwd;
1405     $only_you = FALSE;
1406     
1407     // common settings
1408     $msg = substr($mesg, 6, 128);
1409     $curtime = time();
1410     $dt = date("H:i ", $curtime);
1411
1412     //
1413     //  Compute actions
1414     //
1415
1416     $to_user     = FALSE;
1417     $to_all      = FALSE;
1418     $to_room     = FALSE;
1419     $to_tabl     = FALSE;
1420     $update_room = FALSE;
1421
1422     if (strcmp($msg,  "/tav") == 0 || 
1423         strncmp($msg, "/tav ", 5) == 0) {
1424       do {
1425         if ($user->stat != 'room' || $user->subst != 'sitdown') {
1426           /* MLANG: "<br>Per attivare il messaggio di segnalazione del tavolo occorre essere seduti.<br><br>", "<br>Il messaggio di segnalazione del tavolo &egrave; gi&agrave; attivato.<br><br>", "<br>Il messaggio di segnalazione del tavolo<br>&egrave; disattivato ancora per %d second%s.<br><br>" */
1427           $msg = "<br>Per attivare il messaggio di segnalazione del tavolo occorre essere seduti.<br><br>";
1428           $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1429
1430           break;
1431         }
1432
1433         $table = &$this->table[$user->table];
1434         
1435         if ($table->wag_own != NULL) {
1436           $msg = "<br>Il messaggio di segnalazione del tavolo &egrave; gi&agrave; attivato.<br><br>";
1437           $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1438
1439           break;
1440         }
1441
1442         $dtime = $curtime - $table->wag_tout;
1443         if ($dtime  < EXPIRE_TIME_WAG) {
1444           $msg = sprintf("<br>Il messaggio di segnalazione del tavolo<br>&egrave; disattivato ancora per %d second%s.<br><br>",
1445                          EXPIRE_TIME_WAG - $dtime, (EXPIRE_TIME_WAG - $dtime == 1 ? "o" : "i"));
1446           $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1447
1448           break;
1449         }
1450         
1451         $msg = substr($msg, 5);
1452         
1453         $table->wag_set($user, $msg);
1454         $to_user = sprintf('tra.add(%d, "%s");', $user->table, xcape(sprintf("%s: %s", $user->name, $msg)));
1455         $to_room = $to_user;
1456         
1457       } while (0);
1458     } // /tav chat command
1459
1460     else if (strncmp($msg, "/alarm ", 7) == 0) {
1461       if (strncmp($msg, "/alarm to ", 10) == 0) {
1462         $sp_pos = strpos($msg, " ", 10);
1463         $target = substr($msg, 10, $sp_pos - 10);
1464         $alarm_check = "/alarm to ".$target." ".$G_alarm_passwd." ";
1465       }
1466       else {
1467         $target = "";
1468         $alarm_check = "/alarm ".$G_alarm_passwd." ";
1469       }
1470       do {
1471         if (strncmp($msg, $alarm_check, strlen($alarm_check)) != 0) {
1472           /* MLANG: "<br>La password digitata non &egrave; corretta.<br><br>" */
1473           $msg = "<br>La password digitata non &egrave; corretta.<br><br>";
1474           $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1475
1476           break;
1477         }
1478
1479         /* MLANG: "Alarm <b>%s</b> inviato a <b>%s</b>." */
1480         $to_user = sprintf('chatt_sub("%s", [2, "%s"],"Alarm <b>%s</b> inviato a <b>%s</b>.");', 
1481                            $dt, NICKSERV, xcape(substr($msg, strlen($alarm_check))), 
1482                            ($target == "" ? "tutti" : xcape($target)) );
1483
1484         $msg = sprintf("<br><b>%s<br><br>%s</b><br><br>",
1485                        $dt.NICKSERV, xcape(substr($msg, strlen($alarm_check))));
1486         /* MLANG: "chiudi" */
1487         $to_all = show_notify($msg, 0, "chiudi", 400, 120);
1488       } while (0);
1489     } // /alarm chat command
1490     else if (strncmp($msg, "/garante", 8) == 0) {
1491       if ($user->flags & USER_FLAG_AUTH) {
1492         $to_user = sprintf('authbox(300,200);');
1493       }
1494       else {
1495         /* MLANG: "<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>", "Il nickname deve contenere almeno una lettera dell\'alfabeto o una cifra.", "Nickname <b>%s</b> gi&agrave; in uso." */
1496         $to_user = sprintf('chatt_sub("%s", [2, "%s"],"<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>");', $dt, NICKSERV);
1497       }
1498     }
1499     else if (strncmp($msg, "/nick ", 6) == 0) {
1500       log_main("chatt_send BEGIN");
1501
1502       do {
1503         if (($name_new = validate_name(substr($msg, 6))) == FALSE) {
1504           $to_user = sprintf('chatt_sub("%s", [2,"%s"],"Il nickname deve contenere almeno una lettera dell\'alfabeto o una cifra.");', $dt, NICKSERV);
1505           break;
1506         }
1507
1508         $msg = "COMMAND ".$msg;
1509         for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1510           $user_cur = &$this->user[$i];
1511
1512           if ($user_cur->sess == '')
1513             continue;
1514           if ($user_cur->name == $name_new)
1515             break;
1516           }
1517         if ($i <  MAX_PLAYERS) {
1518           $to_user = sprintf('chatt_sub("%s", [2, "%s"],"Nickname <b>%s</b> gi&agrave; in uso.");', $dt, NICKSERV, xcape($name_new));
1519           break;
1520         }
1521         
1522         /* MLANG: "<b>Non puoi cambiare nick a un tavolo per soli autenticati.</b>", "Il nickname <b>\'%s\'</b> &egrave; gi&agrave; registrato, <b>se il suo proprietario si autentificher&agrave; verrai rinominato d\'ufficio come ghost<i>N</i>.</b>" */
1523         if ($user->flags & USER_FLAG_AUTH) {
1524           if (strcasecmp($user->name,$name_new) != 0) {
1525             if ($user->subst == 'standup' || ($user->subst != 'standup' && $this->table[$user->table]->auth_only == FALSE)) {
1526               $user->flags &= ~USER_FLAG_AUTH; // Remove auth if name changed
1527               for ($i = 0 ; $i < TABLES_N ; $i++) {
1528                 $to_user .= $this->table[$i]->act_content($user);
1529               }
1530             }
1531             else {
1532               $to_user = sprintf('chatt_sub("%s", [2, "%s"],"<b>Non puoi cambiare nick a un tavolo per soli autenticati.</b>");', $dt, NICKSERV);
1533               break;
1534             }
1535           }
1536         }
1537         $user->name = $name_new; // OK - nick changed
1538         /* se nome gia' in uso, segnala cosa potrebbe capitare */
1539         if (($user->flags & USER_FLAG_AUTH) == 0) {
1540           $userdb = new LoginDB();
1541           /* MLANG: "Il nickname <b>\'%s\'</b> &egrave; gi&agrave; registrato, <b>se il suo proprietario si autentificher&agrave; verrai rinominato d\'ufficio come ghost<i>N</i>.</b>" */
1542           if ($userdb->login_exists($name_new)) {
1543             $to_user .= sprintf('chatt_sub("%s", [2, "%s"],"Il nickname <b>\'%s\'</b> &egrave; gi&agrave; registrato, <b>se il suo proprietario si autentificher&agrave; verrai rinominato d\'ufficio come ghost<i>N</i>.</b>");', $dt, NICKSERV, xcape($name_new));
1544           }
1545         }
1546
1547         log_main("chatt_send start set");
1548
1549         $update_room = TRUE;
1550       } while (0);
1551     } // nick chat command
1552
1553     else if (strncmp($msg, "/st ", 4) == 0) {
1554       log_main("chatt_send BEGIN");
1555
1556       do {
1557         $st_str = substr($msg, 4);
1558         
1559         if (strcasecmp($st_str, "normale") == 0) {
1560           $st = USER_FLAG_S_NORM;
1561         }
1562         else if (strcasecmp($st_str, "pausa") == 0) {
1563           $st = USER_FLAG_S_PAU;
1564         }
1565         else if (strcasecmp($st_str, "fuori") == 0) {
1566           $st = USER_FLAG_S_OUT;
1567         }
1568         else if (strcasecmp($st_str, "cane") == 0) {
1569           $st = USER_FLAG_S_DOG;
1570         }
1571         else if (strcasecmp($st_str, "cibo") == 0) {
1572           $st = USER_FLAG_S_EAT;
1573         }
1574         else if (strcasecmp($st_str, "lavoro") == 0) {
1575           $st = USER_FLAG_S_WRK;
1576         }
1577         else if (strcasecmp($st_str, "sigaretta") == 0) {
1578           $st = USER_FLAG_S_SMK;
1579         }
1580         else if (strcasecmp($st_str, "presente") == 0) {
1581           $st = USER_FLAG_S_EYE;
1582         }
1583         else {
1584           /* MLANG: "Questo stato non esiste." */
1585           $to_user = sprintf('chatt_sub("%s", [2,"%s"],"Questo stato non esiste.");', $dt, NICKSERV);
1586           break;
1587         }
1588
1589         log_main("chatt_send start set");
1590         if (($user->flags & USER_FLAG_S_ALL) != $st) {
1591           $update_room = TRUE;
1592           $user->flags = ($user->flags & ~USER_FLAG_S_ALL) | $st;
1593         }
1594       } while (0);
1595     } // nick chat command
1596
1597     else { // normal chat line
1598       if ($curtime < ($user->chat_ban + $user->chat_dlt)) {
1599         $only_you = TRUE;
1600         $user->chat_dlt = $user->chat_dlt * 2; 
1601         if ($user->chat_dlt > 120)
1602           $user->chat_dlt = 120; 
1603       }
1604       else if ($user->chat_lst == $msg)
1605         $only_you = TRUE;
1606       else if ($curtime - $user->chattime[($user->chat_cur + 1) % CHAT_N] < CHAT_ILL_TIME) {
1607         $user->chat_ban = $curtime;
1608         $user->chat_dlt = 5;
1609         $only_you = TRUE;
1610       }
1611       else {
1612         $user->chat_ban = 0;
1613         $user->chat_dlt = 0;
1614       }
1615
1616       if ($only_you) {
1617         $to_user = sprintf('chatt_sub("%s", [%d, "%s"],"%s");', $dt, $user->flags, xcape($user->name), xcape("== chat ban =="));
1618       }
1619       else {
1620         $to_user = sprintf('chatt_sub("%s", [%d, "%s"],"%s");', $dt, $user->flags, xcape($user->name), xcape($msg));
1621         // temporary silentiation for troll (will became array check)
1622         if (strcasecmp($user->name,'JackRokka') != 0 && $user->sess != '47ea653f602e8')
1623           $to_room = $to_user;
1624       }
1625
1626       log_legal($curtime, $user, 
1627                 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$msg);
1628       
1629       $user->chat_lst = "$msg";
1630       $user->chattime[$user->chat_cur % CHAT_N] = $curtime;
1631       $user->chat_cur++;
1632     }
1633
1634     if ($to_all) {
1635       $to_room = $to_all;
1636       $to_tabl = $to_all;
1637     }
1638
1639     //
1640     //  Output to clients
1641     //
1642
1643     if ($to_user != FALSE) {
1644       $user->comm[$user->step % COMM_N] =  "gst.st = ".($user->step+1)."; ";
1645       $user->comm[$user->step % COMM_N] .= $to_user;
1646       $user->step_inc();
1647     }
1648
1649     if ($to_room != FALSE) {
1650       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1651         $user_cur = &$this->user[$i];
1652         if ($target != "" && $user_cur->name != $target)
1653           continue;
1654         //      if ($user_cur->sess == '' || $user_cur->stat != 'room')
1655         if ($user_cur->sess == '' || $user_cur->stat == 'table' || $user_cur == $user)
1656           continue;
1657         
1658         $user_cur->comm[$user_cur->step % COMM_N] =  "gst.st = ".($user_cur->step+1)."; ";
1659         $user_cur->comm[$user_cur->step % COMM_N] .= $to_room; 
1660         $user_cur->step_inc();
1661       }
1662     }
1663     
1664     if ($to_tabl) {
1665       // Before all align times with table timeout
1666       for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1667         $table_cur =& $this->table[$table_idx];
1668         // if the table is complete and exists its shared mem we get the info about users lacc
1669         
1670         if ($table_cur->player_n == PLAYERS_N) {
1671           log_main("PLAYERS == N TABLE ".$table_idx);
1672         
1673           if (($sem = Briskin5::lock_data($table_idx)) != FALSE) { 
1674             log_main("bin5 lock data success");
1675             
1676             $no_recovery = FALSE;
1677             if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
1678               if ($table_cur->table_token != $bri->table_token) {
1679                 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token."  Table: ".$bri->table_token);
1680                 $bri = FALSE;
1681               }
1682             }
1683             
1684             if ($bri != FALSE) {
1685               $bri_table = &$bri->table[0];
1686               for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
1687                 // stat must be "table" by definition
1688                 $bri_user =& $bri->user[$i];
1689               
1690                 if ($target != "" && $bri_user->name != $target)
1691                   continue;
1692                 log_main("writa: ".$user_mesg);
1693                 $bri_user->comm[$bri_user->step % COMM_N] = "gst.st = ".($bri_user->step+1)."; ";
1694                 $bri_user->comm[$bri_user->step % COMM_N] .= $to_tabl;
1695                 $bri_user->step_inc();
1696               }
1697               Briskin5::save_data(&$bri);
1698             }
1699             Briskin5::unlock_data($sem);
1700           } // bri::lock_data
1701         } //  if ($table_cur->player_n == PLAYERS_N) {
1702       } //  for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1703     } // if ($to_tabl == true ...
1704
1705     if ($update_room) {
1706       if ($user->stat == 'room' && $user->subst == 'standup') {
1707         $this->standup_update(&$user);
1708       }
1709       else if ($user->stat == 'room' && $user->subst == 'sitdown') {
1710         log_main("chatt_send pre table update");
1711         $this->table_update(&$user);
1712         log_main("chatt_send post table update");
1713       }
1714     } // if ($update_room ...
1715
1716     return;
1717   } // function chatt_send( ...
1718
1719   function &get_user($sess, &$idx)
1720   {
1721     GLOBAL $PHP_SELF, $G_false;
1722
1723     if (validate_sess($sess)) {
1724       for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1725         if (strcmp($sess, $this->user[$i]->sess) == 0) {
1726           // find it
1727           $idx = $i;
1728           $ret = &$this->user[$i];
1729           return ($ret);
1730         }
1731       }
1732       log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1733       // for ($i = 0 ; $i < MAX_PLAYERS ; $i++) 
1734       // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1735     }
1736     else {
1737       log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1738     }
1739
1740     return ($G_false);
1741   }
1742
1743   
1744
1745   /*
1746    * function &add_user(&$room, &$sess, &$idx, $name, $pass, $ip)
1747    *
1748    * RETURN VALUE:
1749    *   if ($idx >  -1    && ret == FALSE)  =>  duplicated nick
1750    *   if ($idx == -2    && ret == FALSE)  =>  invalid name
1751    *   if ($idx == -3    && ret == FALSE)  =>  wrong password
1752    *   if ($idx == -1    && ret == FALSE)  =>  no space left
1753    *   if ($idx ==  0    && ret == user)   =>  SUCCESS
1754    *   if ($idx == -$idx && ret == user)   =>  SUCCESS (but the login exists in the auth db 
1755    */
1756
1757
1758
1759   function &add_user(&$sess, &$idx, $name, $pass, $ip)
1760   {
1761     GLOBAL $G_false;
1762
1763     $idx = 0;
1764
1765     $authenticate = FALSE;
1766     $login_exists = FALSE;
1767     $ghost = -1;
1768     $ghost_auth = FALSE;
1769     $idx = -1;
1770     $idfree = -1;
1771     
1772     if (($name_new = validate_name($name)) == FALSE) {
1773       $idx = -2;
1774       return ($G_false);
1775     }
1776
1777     log_auth("XXX", sprintf("ARRIVA: [%s] pass:[%s]", $sess, ($pass == FALSE ? "FALSE" : $pass)));
1778     if (validate_sess($sess) == FALSE) 
1779       $sess = "";
1780
1781     /* if pass != FALSE verify the login with pass */
1782     log_auth("XXX", "auth1");
1783     $userdb = new LoginDB();
1784     if ($pass != FALSE) {
1785       log_auth("XXX", "auth2");
1786       $authenticate = $userdb->login_verify($name_new, $pass);
1787       log_auth("XXX", "authenticate: ".($authenticate == TRUE ? "TRUE" : "FALSE"));
1788       
1789       if ($authenticate == FALSE) {
1790         $idx = -3;
1791         return ($G_false);
1792       }
1793     }
1794     else {
1795       $login_exists =  $userdb->login_exists($name_new);
1796     }
1797     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1798       /* free user ? */
1799       if (strcmp($sess, $this->user[$i]->sess) == 0) {
1800         if ($idx == -1)
1801           $idx = $i;
1802       }
1803       if ($idfree == -1 && strcmp("", $this->user[$i]->sess) == 0) {
1804         $idfree = $i;
1805         continue; // NOTE: CHECK IT !!
1806       }
1807       if (strcasecmp($this->user[$i]->name, $name_new) == 0) {
1808         if ($authenticate == FALSE) {
1809           $idx = $i;
1810           break;
1811         }
1812         else {
1813           $ghost = $i;
1814           $ghost_auth = ($this->user[$i]->flags & USER_FLAG_AUTH);
1815         }
1816       }
1817     }
1818     if ($idx == -1)
1819       $idx = $idfree;
1820
1821     log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1822
1823     if ($ghost > -1 && $ghost_auth && $authenticate) {
1824       /* swap session */
1825
1826       $ghost_user =& $this->user[$ghost];
1827       $curtime = time();
1828       $ghost_user->step_inc();
1829       if ($sess == "") {
1830         $sess = uniqid(""); 
1831         $ghost_user->sess = $sess;
1832       }
1833       else {
1834         $ghost_user->sess = $sess;
1835       }
1836       
1837       // If user at the table we need to update the table data too
1838       $table_idx = $ghost_user->table;
1839       if ($ghost_user->stat == "table" && $this->table[$table_idx]->player_n == PLAYERS_N) {
1840         if (($brisem = Briskin5::lock_data($table_idx)) != FALSE) { 
1841           if (($bri = &Briskin5::load_data($table_idx)) != FALSE) {
1842             if ($bri->the_end != TRUE) {
1843               $bri->user[$ghost_user->table_pos]->step_inc();
1844               $bri->user[$ghost_user->table_pos]->sess = $sess;
1845               Briskin5::save_data(&$bri);
1846             }
1847           }
1848           Briskin5::unlock_data($brisem);
1849         }
1850       }
1851
1852       $idx = $ghost;
1853       return ($this->user[$ghost]);
1854     }
1855     else if ($idx != -1 && $i == MAX_PLAYERS) {
1856       /* SUCCESS */
1857       $curtime = time();
1858       if ($sess == "") {
1859         $sess = uniqid("");
1860         $this->user[$idx]->sess = $sess;
1861       }
1862       else {
1863         $this->user[$idx]->sess = $sess;
1864       }
1865       $this->user[$idx]->name = $name_new; // OK - add new user
1866       $this->user[$idx]->stat_set("room");
1867       $this->user[$idx]->step_set(0);
1868       while (array_pop($this->user[$idx]->comm) != NULL);
1869       $this->user[$idx]->subst = "standup";
1870       $this->user[$idx]->lacc =   $curtime;
1871       $this->user[$idx]->laccwr = $curtime;
1872       $this->user[$idx]->bantime = 0;
1873       $this->user[$idx]->ip = $ip;
1874
1875       $this->user[$idx]->flags = ($authenticate ? USER_FLAG_AUTH : 0x00);
1876
1877       if ($ghost > -1) {
1878         log_main("ghost: rename!");
1879         $ghost_user =& $this->user[$ghost];
1880
1881         if ($ghost_auth == FALSE) {
1882           for ($sfx = 1 ; $sfx <= MAX_PLAYERS ; $sfx++) {
1883             $ghostname = 'ghost'.$sfx;
1884             for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1885               if (strcmp("", $this->user[$i]->sess) == 0) 
1886                 continue;
1887               
1888               if (strcmp($this->user[$i]->name, $ghostname) == 0) {
1889                 $ghostname = '';
1890                 break;
1891               }
1892             }
1893             if ($ghostname != '')
1894               break;
1895           }
1896           
1897           $ghost_user->name = $ghostname;
1898           
1899           if ($ghost_user->stat == 'room' && $ghost_user->subst == 'standup') {
1900             $this->standup_update(&$ghost_user);
1901           }
1902           else {
1903             log_main("chatt_send pre table update");
1904             $this->table_update(&$ghost_user);
1905           log_main("chatt_send post table update");
1906           }
1907         } // if ($ghost_auth == FALSE
1908         else {
1909           // FIXME: cacciare il vecchio utente room && table (if needed)
1910           $ghost_user->the_end = TRUE;
1911           $ghost_user->lacc = 0;
1912           $this->garbage_manager(TRUE);
1913         }
1914       } //  if ($ghost > -1) {
1915
1916       log_main(sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1917       $real_idx = $idx;
1918       if ($login_exists)
1919         $idx = -($idx + 1);
1920       return ($this->user[$real_idx]);
1921     }
1922
1923     return ($G_false);
1924   }
1925   
1926   function standup_update(&$user)
1927   {
1928     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1929       $user_cur = &$this->user[$i];
1930       if ($user_cur->sess == '')
1931         continue;
1932
1933       log_main("STANDUP START: ".$user_cur->stat);
1934       
1935       if ($user_cur->stat == 'room') {
1936         $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".$this->standup_content($user_cur);
1937         if ($user_cur == $user) {
1938           $itin = ($user->flags & USER_FLAG_AUTH ? "<i>" : "");
1939           $itou = ($user->flags & USER_FLAG_AUTH ? "</i>" : "");
1940
1941           $user_cur->comm[$user_cur->step % COMM_N] .= sprintf('$("myname").innerHTML = "<b>%s%s%s</b>: ";', 
1942                                                                $itin, xcape($user->name), $itou);
1943         }
1944         log_main("FROM STANDUP: NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
1945         
1946         $user_cur->step_inc();
1947       }
1948     }
1949   }
1950
1951   // Static functions
1952   function &init_data()
1953   {
1954     $room =& new Room();
1955     
1956     return $room;
1957   }
1958   
1959
1960   function &load_data() 
1961   {
1962     GLOBAL $G_false, $sess;
1963     $doexit = FALSE;
1964     do {
1965       if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
1966         log_main("ftok failed");
1967         $doexit = TRUE;
1968         break;
1969       }
1970     
1971       if (($shm_sz = sharedmem_sz($tok)) == -1) {
1972         log_main("shmop_open failed");
1973       }
1974         
1975       if ($shm_sz == -1)
1976         $shm_sz = SHM_DIMS_MIN;
1977
1978       if ($shm = shm_attach($tok, $shm_sz)) {
1979         $room = @shm_get_var($shm, $tok);
1980         
1981         log_only("bri ==  ".($room == FALSE ?   "FALSE" : "TRUE")."  bri ===  ".($room === FALSE ? "FALSE" : "TRUE")."  bri isset ".(isset($room) ?   "TRUE" : "FALSE"));
1982         if (isset($room)) 
1983           log_only("bri count ".count($room));
1984         
1985         if ($room == FALSE) {
1986           log_only("INIT MAIN DATA");
1987           
1988           $room =& Room::init_data();
1989           if (@shm_put_var($shm, $tok, $room) == FALSE) {
1990             log_only("PUT_VAR FALLITA ".strlen(serialize($room)));
1991             log_only(serialize($room));
1992           }
1993         }
1994         $room->shm_sz = $shm_sz;
1995         
1996         shm_detach($shm);
1997       }
1998       $ret = &$room;
1999       return ($ret);
2000     } while (0);
2001     
2002     if ($doexit)
2003       exit();
2004     
2005     return ($G_false);
2006   }
2007   
2008
2009   function save_data(&$room) 
2010   {
2011     GLOBAL $sess;
2012     
2013     $shm =   FALSE;
2014     
2015     // var_dump($room);
2016     
2017     if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) 
2018       return (FALSE);
2019     
2020     while ($room->shm_sz < SHM_DIMS_MAX) {
2021       if (($shm = shm_attach($tok, $room->shm_sz)) == FALSE)
2022         break;
2023       
2024       // log_only("PUT_VAR DI ".strlen(serialize($room)));
2025       if (shm_put_var($shm, $tok, $room) != FALSE) {
2026         shm_detach($shm);
2027         return (TRUE);
2028       }
2029       if (shm_remove($shm) === FALSE) {
2030         log_only("REMOVE FALLITA");
2031         break;
2032       }
2033       shm_detach($shm);
2034       $room->shm_sz += SHM_DIMS_DLT;
2035     } 
2036
2037     if ($shm)
2038       shm_detach($shm);
2039     
2040     return (FALSE);
2041   }
2042
2043   function lock_data()
2044   {
2045     GLOBAL $sess; 
2046     
2047     //  echo "LOCK: ".FTOK_PATH."/main";
2048     //  exit;
2049     if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
2050       return (FALSE);
2051     }
2052     // echo "FTOK ".$tok."<br>";
2053     if (($res = sem_get($tok)) == FALSE) {
2054       return (FALSE);
2055     }
2056     if (sem_acquire($res)) {   
2057       log_lock("LOCK room");
2058       return ($res);
2059     }
2060     else
2061       return (FALSE);
2062   }
2063   
2064   function unlock_data($res)
2065   {
2066     GLOBAL $sess; 
2067     
2068     log_lock("UNLOCK room");
2069
2070     return (sem_release($res));
2071   }
2072
2073
2074   function standup_content($user)
2075   {
2076     $ret = "";
2077     $content = "";
2078     
2079     if ($user->stat != 'room')
2080       return;
2081     
2082     for ($e = 0 , $ct = 0 ; $ct < 4 && $e < MAX_PLAYERS ; $e++) {
2083       if ($this->user[$e]->sess == "" || $this->user[$e]->stat != "room" || $this->user[$e]->name == "")
2084         continue;
2085       $ct++;
2086     }
2087     
2088     // $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
2089     
2090     $content = ' j_stand_cont( [ ';
2091
2092     for ($e = 0 , $ct = 0 ; $e < MAX_PLAYERS ; $e++) {
2093       if ($this->user[$e]->sess == "" || $this->user[$e]->stat != "room" || $this->user[$e]->name == "")
2094         continue;
2095       
2096       $flags = $this->user[$e]->flags;
2097       
2098       if ($this->user[$e]->subst == "standup") {
2099         if ($this->user[$e] == $user) 
2100           { 
2101             $flags |= 1;
2102           }
2103         
2104         $content .= sprintf('%s[ %d, "%s" ]',($ct > 0 ? ', ' : ''), $flags, xcape($this->user[$e]->name));
2105         $ct++;
2106       }
2107     }
2108     $content .= ' ]);';
2109     
2110     return ($content);
2111   }
2112   
2113   function table_content_old($user, $table_idx)
2114   {
2115     $content = "";
2116     $ret = "";
2117     // TODO
2118     //
2119     //   Si possono usare i dati nella classe table
2120     //
2121     
2122     $sess = $user->sess;
2123     $table = &$this->table[$table_idx];
2124     
2125     if ($user->stat != 'room')
2126       return;
2127     
2128     for ($i = 0 ; $i < $table->player_n ; $i++) {
2129       $user_cur = &$this->user[$table->player[$i]];
2130       
2131       if ($user_cur == $user) 
2132         { $hilion = "<b>"; $hilioff = "</b>"; }
2133       else
2134         { $hilion = ""; $hilioff = ""; }
2135       
2136       log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
2137       
2138       $content .= sprintf("%s%s%s<br>",$hilion, xcape($user_cur->name), $hilioff);
2139     }
2140     /*
2141   for ( ; $i < PLAYERS_N ; $i++)
2142     $content .= "<br>";
2143     */
2144     
2145     $ret .= sprintf('$("table%d").innerHTML = "%s";', $table_idx, $content);
2146     
2147     return ($ret);
2148   }
2149
2150
2151   function table_content($user, $table_idx)
2152   {
2153     $content = "";
2154     $ret = "";
2155     // TODO
2156     //
2157     //   Si possono usare i dati nella classe table
2158     //
2159     
2160     $sess = $user->sess;
2161     $table = &$this->table[$table_idx];
2162     
2163     if ($user->stat != 'room')
2164       return;
2165     
2166     $content = "[ ";
2167     for ($i = 0 ; $i < $table->player_n ; $i++) {
2168       $user_cur = &$this->user[$table->player[$i]];
2169
2170       $flags = $user_cur->flags;
2171
2172       if ($user_cur == $user) 
2173         $flags |= 1;
2174       
2175       log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
2176       
2177       $content .= sprintf('%s[ %d, "%s" ]',($i == 0 ? '' : ', '), $flags, xcape($user_cur->name));
2178     }
2179
2180     $content .= ' ]';
2181
2182     $ret .= sprintf('j_tab_cont(%d, %s);', $table_idx, $content);
2183     
2184     return ($ret);
2185   }
2186
2187 } // end class Room
2188
2189 function make_seed()
2190 {
2191   list($usec, $sec) = explode(' ', microtime());
2192   return (float) $sec + ((float) $usec * 100000);
2193 }
2194
2195
2196 function log_only2($log) {
2197   GLOBAL $sess;
2198
2199   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONL2) == 0)
2200     return;
2201
2202   if (isset($sess) == FALSE)
2203     $ssess = "XXXX";
2204   else
2205     $ssess = $sess;
2206
2207   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONL2) == 0)
2208     return;
2209
2210   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2211     fwrite($fp, sprintf("ONL2: [%s] [%s]\n", $ssess, $log));
2212     fclose($fp);
2213   }
2214 }
2215
2216 function log_crit($log) {
2217   GLOBAL $sess;
2218
2219   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_CRIT) == 0)
2220     return;
2221
2222   if (isset($sess) == FALSE)
2223     $ssess = "XXXX";
2224   else
2225     $ssess = $sess;
2226
2227   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_CRIT) == 0)
2228     return;
2229
2230   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2231     fwrite($fp, sprintf("CRIT: [%s] [%s]\n", $ssess, $log));
2232     fclose($fp);
2233   }
2234 }
2235
2236 function log_only($log) {
2237   GLOBAL $sess;
2238
2239   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONLY) == 0)
2240     return;
2241
2242   if (isset($sess) == FALSE)
2243     $ssess = "XXXX";
2244   else
2245     $ssess = $sess;
2246
2247   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONLY) == 0)
2248     return;
2249
2250   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2251     fwrite($fp, sprintf("ONLY: [%s] [%s]\n", $ssess, $log));
2252     fclose($fp);
2253   }
2254 }
2255
2256 function log_main($log) {
2257   GLOBAL $sess;
2258
2259   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_MAIN) == 0)
2260     return;
2261
2262   if (isset($sess) == FALSE)
2263     $ssess = "XXXX";
2264   else
2265     $ssess = $sess;
2266
2267   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_MAIN) == 0)
2268     return;
2269
2270   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2271     fwrite($fp, sprintf("MAIN: [%s] [%s]\n", $ssess, $log));
2272     fclose($fp);
2273   }
2274 }
2275
2276 function log_rd($log) {
2277   GLOBAL $sess;
2278
2279   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_READ) == 0)
2280     return;
2281
2282   if (isset($sess) == FALSE)
2283     $ssess = "XXXX";
2284   else
2285     $ssess = $sess;
2286
2287   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_READ) == 0)
2288     return;
2289
2290   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2291     fwrite($fp, sprintf("READ: [%s] [%s]\n", $ssess, $log));
2292     fclose($fp);
2293   }
2294 }
2295
2296 function log_rd2($log) {
2297   GLOBAL $sess;
2298
2299   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_REA2) == 0)
2300     return;
2301
2302   if (isset($sess) == FALSE)
2303     $ssess = "XXXX";
2304   else
2305     $ssess = $sess;
2306       
2307   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_REA2) == 0)
2308     return;
2309
2310   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2311     fwrite($fp, sprintf("REA2: [%s] [%s]\n", $ssess, $log));
2312     fclose($fp);
2313   }
2314 }
2315
2316 function log_send($log) {
2317   GLOBAL $sess;
2318
2319   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SEND) == 0)
2320     return;
2321
2322   if (isset($sess) == FALSE)
2323     $ssess = "XXXX";
2324   else
2325     $ssess = $sess;
2326       
2327   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SEND) == 0)
2328     return;
2329
2330   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2331     fwrite($fp, sprintf("SEND: [%s] [%s]\n", $ssess, $log));
2332     fclose($fp);
2333   }
2334 }
2335
2336 function log_lock($log) {
2337   GLOBAL $sess;
2338
2339   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOCK) == 0)
2340     return;
2341
2342   if (isset($sess) == FALSE)
2343     $ssess = "XXXX";
2344   else
2345     $ssess = $sess;
2346
2347   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOCK) == 0)
2348     return;
2349
2350   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2351     fwrite($fp, sprintf("LOCK: [%s] [%s]\n", $ssess, $log));
2352     fclose($fp);
2353   }
2354 }
2355
2356 function log_wr($log) {
2357   GLOBAL $sess;
2358
2359   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_WRIT) == 0)
2360     return;
2361
2362   if (isset($sess) == FALSE)
2363     $ssess = "XXXX";
2364   else
2365     $ssess = $sess;
2366       
2367   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_WRIT) == 0)
2368     return;
2369
2370   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2371     fwrite($fp, sprintf("WRIT: [%s] [%s]\n", $ssess, $log));
2372     fclose($fp);
2373   }
2374 }
2375
2376 function log_load($log) {
2377   GLOBAL $sess;
2378
2379   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOAD) == 0)
2380     return;
2381
2382   if (isset($sess) == FALSE)
2383     $ssess = "XXXX";
2384   else
2385     $ssess = $sess;
2386
2387   if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOAD) == 0)
2388     return;
2389       
2390   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2391     fwrite($fp, sprintf("LOAD: [%s] [%s]\n", $ssess, $log));
2392     fclose($fp);
2393   }
2394 }
2395
2396 function log_auth($sess, $log) {
2397   if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_AUTH) == 0)
2398     return;
2399
2400   if (( (BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_AUTH) == 0)
2401     return;
2402
2403   if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2404     fwrite($fp, sprintf("AUTH: [%s] [%d] [%s]\n", $sess, time(), $log));
2405     fclose($fp);
2406   }
2407 }
2408
2409
2410 // function log_legal($curtime, $sess, $name, $where, $mesg) 
2411 function log_legal($curtime, &$user, $where, $mesg) 
2412 {
2413   GLOBAL $_SERVER;
2414
2415   if (($fp = @fopen(LEGAL_PATH."/legal.log", 'a')) != FALSE) {
2416     /* Unix time | session | nickname | IP | where was | mesg */
2417     fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
2418                         ($user->flags & USER_FLAG_AUTH ? 'A' : 'N'),
2419                         $user->name, $_SERVER['REMOTE_ADDR'], $where , $mesg));
2420     fclose($fp);
2421   }
2422 }
2423
2424
2425
2426
2427 function lock_banlist()
2428 {
2429   if (($tok = @ftok(FTOK_PATH."/main", "L")) == -1) {
2430     echo "FTOK FAILED";
2431     exit;
2432   }
2433   if (($res = sem_get($tok)) == FALSE) {
2434     echo "SEM_GET FAILED";
2435     exit;
2436   }
2437   if (sem_acquire($res)) 
2438     return ($res);
2439   else
2440     return (FALSE);
2441 }
2442
2443 function unlock_banlist($res)
2444 {
2445   return (sem_release($res));
2446 }
2447
2448 function table_act_content_old($isstanding, $sitted, $table, $cur_table)
2449 {
2450   $ret = "";
2451
2452   if ($isstanding) {
2453     if ($sitted < PLAYERS_N) {
2454       $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xhenter%d\\"  value=\\"Mi siedo.\\" onclick=\\"act_sitdown(%d);\\">', $table, $table);
2455     }
2456   }
2457   else {
2458     if ($table == $cur_table)
2459       $ret = sprintf('<input type=\\"button\\" class=\\"button\\" name=\\"xwakeup\\"  value=\\"Mi alzo.\\" onclick=\\"act_wakeup();\\">');
2460     else
2461       $ret = "";
2462   }
2463   return ($ret);
2464 }
2465
2466 function table_act_content($isstanding, $sitted, $table, $cur_table, $allowed)
2467 {
2468   $ret = "";
2469
2470   if ($isstanding) {
2471     if ($sitted < PLAYERS_N) {
2472       if ($allowed)
2473         $act = 'sit';
2474       else
2475         $act = 'reserved';
2476     }
2477   }
2478   else {
2479     if ($table == $cur_table)
2480       $act = 'wake';
2481     else
2482       $act = 'none';
2483   }
2484
2485   if ($act != '')
2486     $ret = sprintf('j_tab_act_cont(%d, \'%s\');', $table, $act);
2487
2488   return ($ret);
2489 }
2490
2491 function show_notify($text, $tout, $butt, $w, $h)
2492 {
2493   log_main("SHOW_NOTIFY: ".$text);
2494   return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
2495 }
2496
2497 function multoval($mult)
2498 {
2499   if ($mult == 2)
2500     return ("doppio");
2501   else if ($mult == 4)
2502     return ("quadruplo");
2503   else
2504     return (sprintf("%d-plo", $mult));
2505 }
2506
2507
2508 function root_wellcome($user)
2509 {
2510   GLOBAL $root_wellarr;
2511   $ret = "";
2512
2513   $curtime = time();
2514   $dt = date("H:i ", $curtime);
2515     
2516   for ($i = 0 ; $i < count($root_wellarr) ; $i++)
2517     $ret .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, str_replace('"', '\"', $root_wellarr[$i]));
2518
2519   return ($ret);
2520 }
2521
2522
2523
2524 function validate_sess($sess) 
2525 {
2526   if (strlen($sess) == SESS_LEN) 
2527     return (TRUE);
2528   else
2529     return (FALSE);
2530 }
2531
2532 function validate_name($name) 
2533 {
2534   $name_new = str_replace(' ', '_', substr(trim($name),0,12));
2535
2536   for ($i = 0 ; $i < strlen($name_new) ; $i++) {
2537     $c = $name_new[$i];
2538     if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
2539       return ($name_new);
2540   }
2541
2542   return (FALSE);
2543 }
2544
2545 function playsound($filename)
2546 {
2547   return (sprintf('playsound("flasou", "%s");', $filename));
2548 }
2549
2550 function secstoword($secs)
2551 {
2552   $mins = floor($secs / 60);
2553   $secs = $secs % 60;
2554   if ($mins > 0) 
2555     $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
2556   
2557   if ($secs > 0)
2558     $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));
2559   
2560   return ($ret);
2561 }
2562
2563 function sharedmem_sz($tok)
2564 {
2565   if (($shm_id = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
2566     log_main("shmop_open failed");
2567     return (-1);
2568   }
2569   $shm_sz = shmop_size($shm_id);
2570   shmop_close($shm_id);
2571   
2572   log_main("shm_sz: ".$shm_sz."   SHM_DIMS: ".SHM_DIMS);
2573   return ($shm_sz);
2574 }    
2575
2576 class Warrant {
2577   function lock_data()
2578   {
2579     GLOBAL $sess; 
2580     
2581     if (($tok = @ftok(FTOK_PATH."/warrant", "B")) == -1) {
2582       return (FALSE);
2583     }
2584     // echo "FTOK ".$tok."<br>";
2585     if (($res = sem_get($tok)) == FALSE) {
2586       return (FALSE);
2587     }
2588     if (sem_acquire($res)) {   
2589       log_lock("LOCK room");
2590       return ($res);
2591     }
2592     else
2593       return (FALSE);
2594   }
2595   
2596   function unlock_data($res)
2597   {
2598     GLOBAL $sess; 
2599     
2600     log_lock("UNLOCK room");
2601     
2602     return (sem_release($res));
2603   }
2604
2605   
2606 }
2607 ?>