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