5 * Copyright (C) 2006-2011 Matteo Nastasi
6 * mailto: nastasi@alternativeoutput.it
7 * matteo.nastasi@milug.org
8 * web: http://www.alternativeoutput.it
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.
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.
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('TABLES_AUTH_N', 4);
32 define('PLAYERS_N', 3);
33 define('MAX_POINTS', 5);
34 define('MAX_PLAYERS', (20 + (PLAYERS_N * TABLES_N)));
35 define('SHM_DIMS_MIN', (50000 + 10000 * TABLES_N + 15000 * MAX_PLAYERS));
36 define('SHM_DIMS_MAX', SHM_DIMS_MIN + 1048576);
37 define('SHM_DIMS_DLT', 65536);
39 define('SHM_DIMS_U_MIN', 4096);
40 define('SHM_DIMS_U_MAX', 65536);
41 define('SHM_DIMS_U_DLT', 4096);
44 define('COMM_GEN_N', 50);
47 define('CHAT_ILL_TIME', 6);
49 define('SESS_LEN', 13);
50 define('STREAM_TIMEOUT', 60);
51 define('EXPIRE_TIME_RD', 180);
52 define('EXPIRE_TIME_SMAMMA', 360);
53 define('EXPIRE_TIME_WAG', 10);
54 define('WAKEUP_TIME', 12);
55 // BAN_TIME da allineare anche in commons.js
56 define('BAN_TIME', 3600);
57 define('GARBAGE_TIMEOUT', 10);
58 define('NICKSERV', "<i>BriskServ</i>");
60 define('LOCK_SHARE_MAX', 10000);
62 define('DBG_ONL2', 0x0001);
63 define('DBG_ONLY', 0x0002);
64 define('DBG_MAIN', 0x0004);
65 define('DBG_READ', 0x0008);
66 define('DBG_REA2', 0x0010);
67 define('DBG_SEND', 0x0020);
68 define('DBG_LOCK', 0x0040);
69 define('DBG_WRIT', 0x0080);
70 define('DBG_LOAD', 0x0100);
71 define('DBG_AUTH', 0x0200);
72 define('DBG_CRIT', 0x0400);
73 define('DBG_LMOP', 0x0800);
74 define('DBG_TRAC', 0x1000);
75 define('DBG_SHME', 0x2000);
76 // NOTE: BRISK DEBUG must be a numerical constant, not the result of operations on symbols
77 define('BRISK_DEBUG', 0x0800);
79 define('BRISK_SINGLE_DEBUG',0);
80 define('BRISK_SINGLE_SESS', "");
81 define('DEBUGGING', "no-debugging");
83 require_once("$DOCUMENT_ROOT/Etc/".BRISK_CONF);
85 $mlang_brisk = array( 'btn_backstand'=> array( 'it' => 'torna in piedi',
86 'en' => 'back standing' ),
87 'btn_close' => array( 'it' => 'chiudi',
90 'tit_all' => array( 'it' => 'tutti',
93 'tabtout_a'=> array( 'it' => '<br>Sei stato inattivo per ',
94 'en' => '<br>You are being idle for ' ),
95 'tabtout_b'=> array( 'it' => ' minuti. <br><br>Quindi ritorni tra i <b>Giocatori in piedi</b>.',
96 'en' => ' minutes. <br><br>Then you return with the <b>standing players</b>.'),
97 'tickmust' => array( 'it' => '<br>Per attivare il messaggio di segnalazione del tavolo occorre essere seduti.<br><br>',
98 'en' => '<br>To activate the signalling message of the table it\'s necessary to be sitting<br><br>'),
99 'tickjust' => array( 'it' => '<br>Il messaggio di segnalazione del tavolo è già attivato.<br><br> ',
100 'en' => 'EN<br>Il messaggio di segnalazione del tavolo è già attivato.<br><br> '),
101 'tickwait' => array( 'it' => '<br>Il messaggio di segnalazione del tavolo<br>è disattivato ancora per %d second%s.<br><br>',
102 'en' => 'EN<br>The signalling message of the table<br>will be deactivated for %d second%s.<br><br>'),
103 'alarpass' => array( 'it' => '<br>La password digitata non è corretta.<br><br>',
104 'en' => '<br>The entered password is not correct.<br><br>'),
105 'alarret' => array( 'it' => '"Alarm \\"<b>%s</b>\\" inviato a <b>%s</b>."',
106 'en' => '"Alarm \\"<b>%s</b>\\" sent to <b>%s</b>."'),
107 'authmust' => array( 'it' => '<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>',
108 'en' => '<b>To authenticate someone you have to be authenticated.</b>'), // on your turn
109 'mesgmust' => array( 'it' => '<b>Per inviare un messaggio devi essere autenticato.</b>',
110 'en' => '<b>To send a message you have to be authenticated.</b>'),
111 'nickmust' => array( 'it' => 'Il nickname deve contenere almeno una lettera dell\'alfabeto o una cifra.',
112 'en' => 'The nickname have to contain at least one letter or one number.'),
113 'nickdupl' => array( 'it' => 'Nickname <b>%s</b> già in uso.',
114 'en' => 'The nickname <b>%s</b> is already in use.'),
115 'authchan' => array( 'it' => '<b>Non puoi cambiare nick a un tavolo per soli autenticati o se sei in modalità isolata.</b>',
116 'en' => '<b>You can\'t change your nickname into a table for only authenticated or if you are in isolation mode.</b>'),
117 'nickjust' => array( 'it' => 'Il nickname <b>\'%s\'</b> è già registrato, <b>se il suo proprietario si autentificherà verrai rinominato d\'ufficio come ghost<i>N</i>.</b>',
118 'en' => 'The nickname <b>\'%s\'</b> is already registered, <b>if its proprietary will authenticate you will named again officially ghost<i>N</i>.</b>'), // FIXME: him ???
119 'statunkn' => array( 'it' => 'Questo stato non esiste.',
120 'en' => 'This state don\'t exists.'),
121 'tabincon' => array( 'it' => '<br>I dati del tavolo n° %d sono inconsistenti, verranno resettati.<br><br>Torni in piedi.<br><br>',
122 'en' => 'EN <br>I dati del tavolo n° %d sono inconsistenti, verranno resettati.<br><br>Torni in piedi.<br><br>'),
123 'listmust' => array( 'it' => '<b>Per andare in isolamento non bisogna essere seduti a tavoli non riservati.</b>',
124 'en' => '<b>To go to isolation you must don\'t stay on not reserved tables</b>'),
126 'tit_onauth'=>array( 'it' => '(solo aut.)',
127 'en' => '(only aut.)'),
128 'tit_onisol'=>array( 'it' => '(isolam.to)',
129 'en' => '(isolation)'),
130 'db_failed' =>array('it' => '<br>Il collegamento al database è fallito.<br>Temporaneamente tutte le autenticazioni verranno sospese, accederai a Brisk come un utente normale.<br><br>Ci scusiamo per il disagio.',
131 'en' => 'Connection to the database failed<br>All authentications are suspended temporarly, you login as normal user.<br>We are about the limitation')
136 $G_lng = langtolng($G_lang);
138 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
139 $G_brisk_version = "3.6.3";
141 /* MLANG: ALL THE INFO STRINGS IN brisk.phh */
142 $root_wellarr = array( 'it' => array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: nuovo sistema di evidenziazione degli utenti registrati.',
143 'Se vuoi iscriverti alla <a target="_blank" href="mailto:ml-briscola+subscribe@milug.org">Mailing List</a>, cliccala!' ),
144 'en' => array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NEWS</b>: usage of reader/writer locking instead of generic exclusive locking.',
145 'If you want to subscribe our <a target="_blank" href="ml-briscola+subscribe@milug.org">Mailing List</a>, click it!' ) );
147 $G_room_help = array( 'it' => '
148 <div style=\\"text-align: left; padding: 8px;\\">
149 <b>Descrizione</b><br>
150 Questa è un\'implementazione della briscola in cinque, così come è spiegata su
151 <a target=\\"_blank\\" href=\\"http://it.wikipedia.org/wiki/Briscola#Gioco_a_5\\">Wikipedia</a>; in breve è la variante con l\'asta prima sulla carta e poi sui punti.<br><br>
152 <b>Configurazione del browser.</b><br>
153 Occorre abilitare i cookies.<br>
155 <b>Uso del sito</b><br>
156 Potete sedervi a un tavolo o rimanere in piedi.<br>
157 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
160 All\'inizio vengono distribuite le carte e parte l\'asta; per partecipare all\'asta, quando sarà 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>
161 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
162 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
163 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
164 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
165 Dopo che è 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.
167 <dt><b>Comandi della chat</b>
168 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
169 <dd><b>/tav <i><frase di invito></i></b> - invito per gli altri giocatori al tavolo dove si è seduti
170 <dd><b>/st <i><stato></i></b> - cambia l\'icona associata al tuo user; <i>stato</i> può valere: \\"normale\\", \\"fuori\\", \\"pausa\\", \\"cibo\\", \\"cane\\", \\"lavoro\\", \\"presente\\" oppure \\"sigaretta\\"
171 <dd><b>/authreq</b> - se si è autenticati permette di garantire per un utente fidato
172 <dd><b>/mesgtoadm</b> - se si è autenticati permette di lasciare un messaggio all\'amministratore del sito
173 <dd><b>/listen <all or auth></b> - se si è autenticati permette leggere solo i messaggi degli altri autenticati (auth) o di tutti (all)
179 <div style=\\"text-align: left; padding: 8px;\\">
180 <b>EN Descrizione</b><br>
181 EN Questa è un\'implementazione della briscola in cinque, così come è spiegata su
182 <a target=\\"_blank\\" href=\\"http://it.wikipedia.org/wiki/Briscola#Gioco_a_5\\">Wikipedia</a>; in breve è la variante con l\'asta prima sulla carta e poi sui punti.<br><br>
183 <b>EN Configurazione del browser.</b><br>
184 Occorre abilitare i cookies.<br>
186 <b>Uso del sito</b><br>
187 Potete sedervi a un tavolo o rimanere in piedi.<br>
188 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
191 All\'inizio vengono distribuite le carte e parte l\'asta; per partecipare all\'asta, quando sarà 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>
192 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
193 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
194 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
195 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
196 Dopo che è 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.
198 <dt><b>Comandi della chat</b>
199 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
200 <dd><b>/tav <i><frase di invito></i></b> - invito per gli altri giocatori al tavolo dove si è seduti
201 <dd><b>/st <i><stato></i></b> - cambia l\'icona associata al tuo user; <i>stato</i> può valere: \\"normale\\", \\"fuori\\", \\"pausa\\", \\"cibo\\", \\"cane\\", \\"lavoro\\", \\"presente\\" oppure \\"sigaretta\\"
202 <dd><b>/authreq</b> - se si è autenticati permette di garantire per un utente fidato
203 <dd><b>/mesgtoadm</b> - se si è autenticati permette di lasciare un messaggio all\'amministratore del sito
204 <dd><b>/listen <all or auth></b> - se si è autenticati permette leggere solo i messaggi degli altri autenticati (auth) o di tutti (all)
210 $G_room_passwdhowto = array( 'it' => '<br><h2>Come registrarsi su Brisk</h2>
211 <div style=\\"text-align: left; padding: 8px;\\">
212 Attualmente ci sono due metodi per ottenere una password sul sito:<br><br>
214 <li><b>Facendosi garantire da un utente di Brisk che già possidede una password</b><br><br>
215 <li><b>Auto-garantendosi utilizzando uno dei seguenti sistemi di identificazione digitale:</b><br><br>
217 <li>Carta Regionale dei Servizi della Lombardia (la tessera sanitaria)
218 <li>Carta Regionale dei Servizi del Friuli Venezia Giulia (la tessera sanitaria)
219 <li>Smart card di InfoCamere
222 <b>Per auto-garantisi occorre possedere:</b><br><br>
224 <li>il codice PIN della propria carta
225 <li>il lettore di smart-card per collegare la carta al PC (acquistabile di solito presso le edicole)
228 <b>Per effettuare la registrazione collegarsi al sito:</b><br><br>
230 <dd><a class=\\"flat\\" target=\\"_blank\\" href=\\"https://brisk.mine.nu\\">https://brisk.mine.nu</a>
233 Se sei in possesso di una carta che permette l\'identificazione via internet che non è nell\'elenco qui sopra
234 <a class=\\"flat\\" href=\\"mailto:authadmbrisk@alternativeoutput.it\\">fai una segnalazione</a>.
239 'en' => '<br><h2>EN Come registrarsi su Brisk</h2>
240 <div style=\\"text-align: left; padding: 8px;\\">
241 EN Attualmente ci sono due metodi per ottenere una password sul sito:<br><br>
243 <li><b>Facendosi garantire da un utente di Brisk che già possidede una password</b><br><br>
244 <li><b>Auto-garantendosi utilizzando uno dei seguenti sistemi di identificazione digitale:</b><br><br>
246 <li>Carta Regionale dei Servizi della Lombardia (la tessera sanitaria)
247 <li>Carta Regionale dei Servizi del Friuli Venezia Giulia (la tessera sanitaria)
250 <b>Per auto-garantisi occorre possedere:</b><br><br>
252 <li>il codice PIN della propria carta
253 <li>il lettore di smart-card per collegare la carta al PC (acquistabile di solito presso le edicole)
256 <b>Per effettuare la registrazione collegarsi al sito:</b><br><br>
258 <dd><a class=\\"flat\\" target=\\"_blank\\" href=\\"https://brisk.mine.nu\\">https://brisk.mine.nu</a>
261 Se sei in possesso di una carta che permette l\'identificazione via internet che non è nell\'elenco qui sopra
262 <a class=\\"flat\\" href=\\"mailto:authadmbrisk@alternativeoutput.it\\">fai una segnalazione</a>.
268 <dd>Seguendo la procedura di auto-garanzia all\'url: <a href="https://brisk.mine.nu">https://brisk.mine.nu</a>
272 $G_room_about = array( 'it' => '<br>
273 <div id=\\"header\\" class=\\"header\\">
274 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
275 briscola chiamata in salsa ajax
277 <br><b>version '.$G_brisk_version.'</b><br><br>
278 Copyright 2006-2012 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>',
280 <div id=\\"header\\" class=\\"header\\">
281 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
282 declaration briscola in ajax sauce <b>(Beta)</b>
284 <br><b>version '.$G_brisk_version.'</b><br><br>
285 Copyright 2006-2009 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>');
289 for ($i = 0; $i < ob_get_level(); $i++)
291 ob_implicit_flush(1);
295 function file_lock($fname, $is_exclusive)
297 if (($res = @fopen($fname, "r+")) == FALSE) {
301 if (flock($res, ($is_exclusive ? LOCK_EX : LOCK_SH)) == FALSE) {
309 function file_unlock($res)
312 flock($res, LOCK_UN);
317 function webservers_exceeded()
319 return(file_exists(PROXY_PATH."/webservers_exceded.flag"));
322 function webservers_check()
324 GLOBAL $G_webserver_max;
326 /* FIXME: check all procs expirations */
331 $dh = opendir('/proc');
332 while (($file = readdir($dh)) !== false) {
333 if (preg_match('/[0-9]+/', $file)) {
334 $cmdline = explode("\0", file_get_contents('/proc/'.$file.'/cmdline'));
335 // echo "xxx".$cmdline[0].$n;
336 if (strstr('/usr/sbin/apache2', $cmdline[0]) != FALSE) {
337 // echo "yyy".$cmdline[0].$n;
344 if ($ct >= $G_webserver_max) {
345 touch(PROXY_PATH."/webservers_exceded.flag");
348 unlink(PROXY_PATH."/webservers_exceded.flag");
353 $escpush_from = array("\\", "\"");
354 $escpush_to = array("\\\\", "\\\"");
357 GLOBAL $escpush_from, $escpush_to;
359 return str_replace($escpush_from, $escpush_to, $s);
362 $escinp_from = array( "\"" );
363 $escinp_to = array( """ );
365 function escinput($s)
367 GLOBAL $escinp_from, $escinp_to;
369 return str_replace($escinp_from, $escinp_to, $s);
374 return htmlentities($s);
377 function esclfhtml($s)
379 return str_replace("\n", "<br>\n", htmlentities($s));
383 function langtolng($lang)
387 return ($G_lang == 'en' ? '-en' : '');
390 function csplitter($in, $sep)
396 for ($i = 0 ; $i < strlen($in) ; $i++) {
397 $ini = substr($in, $i, 1);
401 else if ($ini == $sep) {
420 $from = array ( '\\', '@', '|' );
421 $to = array ( '\\\\', '@', '¦' );
423 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
428 $from = array ( '\\', '|', "\t", "\n");
429 $to = array ( '\\\\', '\\|', "\\t", "\\n");
431 return (str_replace($from, $to, $s));
434 function xcapemesg($s)
436 $from = array ( "\n");
437 $to = array ( "\\n");
439 return (str_replace($from, $to, $s));
449 function getbyid($idx)
451 return ($this->el[$idx]);
454 function setbyid($idx, $v)
456 $this->el[$idx] = $v;
465 var $auth_only; // se tavolo riservato o libero
472 var $table_start; // information field
480 function create($idx)
482 if (($thiz = new Table()) == FALSE)
486 $thiz->player = array();
488 $thiz->auth_only = FALSE;
494 $thiz->table_token = "";
495 $thiz->table_start = 0;
497 $thiz->wakeup_time = 0;
504 $this->idx = $from->idx;
505 $this->player = array();
506 for ($i = 0 ; $i < $from->player_n ; $i++)
507 $this->player[$i] = $from->player[$i];
508 $this->player_n = $from->player_n;
510 log_main("PLAYER_N - parent::copy.".$this->player_n);
512 $this->auth_only = $from->auth_only;
514 $this->wag_own = $from->wag_own;
515 $this->wag_com = $from->wag_com;
516 $this->wag_tout = $from->wag_tout;
518 $this->table_token = $from->table_token;
519 $this->table_start = $from->table_start;
521 $this->wakeup_time = $from->wakeup_time;
524 function myclone($from)
526 if (($thiz = new Table()) == FALSE)
534 function spawn($from)
536 if (($thiz = new Table()) == FALSE)
539 $thiz->idx = $from->idx;
540 $thiz->player = array();
541 for ($i = 0 ; $i < $from->player_n ; $i++)
542 $thiz->player[$i] = $i;
543 $thiz->player_n = $from->player_n;
545 $thiz->auth_only = $from->auth_only;
547 $thiz->wag_own = $from->wag_own;
548 $thiz->wag_com = $from->wag_com;
549 $thiz->wag_tout = $from->wag_tout;
551 $thiz->table_token = $from->table_token;
552 $thiz->table_start = $from->table_start;
554 $thiz->wakeup_time = $from->wakeup_time;
559 function wag_set($user_idx, $mesg)
563 $this->wag_own = $user_idx;
564 $this->wag_com = $mesg;
568 function wag_reset($timeout)
570 log_main("WAG_RESET");
572 unset($this->wag_own);
575 $this->wag_tout = $timeout;
578 function player_get($idx)
580 return ($this->player[$idx]);
583 function player_set($idx, $player)
585 $this->player[$idx] = $player;
588 function user_add($idx)
590 $this->player[$this->player_n] = $idx;
593 return ($this->player_n - 1);
596 function user_rem($room, $user)
598 $tabpos = $user->table_pos;
600 /* verifico la consistenza dei dati */
601 if ($room->user[$this->player[$tabpos]] == $user) {
603 /* aggiorna l'array dei giocatori al tavolo. */
604 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
605 $this->player[$i] = $this->player[$i+1];
606 $user_cur = $room->user[$this->player[$i]];
607 $user_cur->table_pos = $i;
612 log_main("INCONSISTENCY ON TABLE.");
618 // $ret .= table_act_content(($user->subst == 'standup'), $this->table[$i]->player_n, $i, $user->table,
619 // ($this->table[$i]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
621 // function act_content($isstanding, $sitted, $table, $cur_table, $allowed)
622 function act_content($user)
625 $isstanding = ($user->subst == 'standup');
626 $sitted = $this->player_n;
628 $cur_table = $user->table;
632 if ($sitted < PLAYERS_N) {
633 if ($this->auth_only) {
634 if ($user->flags & USER_FLAG_AUTH)
648 if ($table == $cur_table)
655 $ret = sprintf('j_tab_act_cont(%d, \'%s\');', $table, $act);
669 var $comm; // commands for many people
670 var $step; // current step of the comm array
671 // externalized var $garbage_timeout;
675 $this->user = array();
676 $this->table = array();
678 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
679 $this->user[$i] = User::create($this, $i, "", "");
682 for ($i = 0 ; $i < TABLES_N ; $i++) {
683 $this->table[$i] = Table::create($i);
686 $row = ( (((int)($i / 4)) % 2) == 0 );
687 $col = ($i % 2 == 0);
688 $this->table[$i]->auth_only = (($row && $col) || (!$row && !$col));
691 $this->table[$i]->auth_only = FALSE;
694 if ($i < TABLES_AUTH_N)
695 $this->table[$i]->auth_only = TRUE;
697 $this->table[$i]->auth_only = FALSE;
699 // externalized $this->garbage_timeout = 0;
700 Room::garbage_time_expire_set(0);
701 $this->shm_sz = SHM_DIMS_MIN;
704 function garbage_manager($force)
706 GLOBAL $G_lang, $mlang_brisk, $G_base;
710 log_rd2("garbage_manager START");
712 /* Garbage collector degli utenti in timeout */
715 // externalized if ($force || $this->garbage_timeout < $curtime) {
716 if (!$force && !Room::garbage_time_is_expired($curtime)) {
721 // FIXME BRISK4: include for each kind of table
722 require_once("${G_base}briskin5/Obj/briskin5.phh");
724 // Before all align times with table timeout
725 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
726 $table_cur = $this->table[$table_idx];
727 // if the table is complete and exists its shared mem we get the info about users lacc
729 if ($table_cur->player_n == PLAYERS_N) {
730 log_main("PLAYERS == N TABLE ".$table_idx);
732 if (($sem = Bin5::lock_data(TRUE, $table_idx)) != FALSE) {
733 log_main("bin5 lock data success");
735 $no_recovery = FALSE;
736 if (($bri = Bin5::load_data($table_idx)) != FALSE) {
737 if ($table_cur->table_token != $bri->table_token) {
738 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
739 log_main("ERROR: not matching table_start. Room: ".$table_cur->table_start." Table: ".$bri->table_start);
749 log_main("garbage_manager: bri loaded successfully.");
750 $bri->garbage_manager(TRUE);
752 $bri_table = $bri->table[0];
754 // is the end of the table
755 if ($bri->the_end == TRUE) {
757 * DESTROY OF FINISHED TABLE && MOVE PLAYER TO ROOM AGAIN
759 log_main("garbage_manager: INSIDE THE END.");
761 $plist = "$table_cur->table_token|$table_cur->idx|$table_cur->player_n";
762 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
763 $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
766 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
767 // stat must be "table" by definition
768 $user_cur = $this->user[$table_cur->player[$i]];
769 $bri_user = $bri->user[$i];
771 $user_cur->subst = $bri_user->subst;
772 $user_cur->step = $bri_user->step;
773 $user_cur->lacc = $bri_user->lacc;
774 $user_cur->laccwr = $bri_user->lacc;
775 $user_cur->bantime = $bri_user->bantime;
778 log_legal($curtime, 'xxx', $user_cur, "STAT:DESTROY_GAME", $plist);
780 $this->room_join_wakeup($user_cur, FALSE, 0);
781 $table_cur->table_token = "";
782 $table_cur->wakeup_time = $curtime + WAKEUP_TIME;
784 $bri->destroy_data($table_idx);
787 log_main("gm:: save_data");
789 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
790 $this->user[$table_cur->player[$i]]->lacc = $bri->user[$i]->lacc;
793 Bin5::save_data($bri);
795 } // else if (($bri = Bin5::load_data($table_idx)) != FALSE) {
796 else if ($no_recovery == FALSE) {
797 log_crit("ERROR: table ".$table_idx." unrecoverable join");
799 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
800 $user_cur = $this->user[$table_cur->player[$i]];
801 $user_cur->subst = "shutdowner";
802 $user_cur->step_inc();
804 $ret = sprintf('stat = "%s"; subst = "%s";', $user_cur->stat, $user_cur->subst);
805 $ret .= "gst.st = ".($user_cur->step+1)."; ";
806 // MLANG <br>I dati del tavolo n° ".$user_cur->table." sono inconsistenti, verranno resettati.<br><br>Torni in piedi.<br><br>
807 $prestr = sprintf($mlang_brisk['tabincon'][$G_lang], $user_cur->table);
808 $ret .= show_notify($prestr, 2000, $mlang_brisk['btn_close'][$G_lang], 400, 110);
809 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
810 $user_cur->step_inc();
813 $plist = "$table_cur->table_token|$user_cur->table|$table_cur->player_n";
814 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
815 $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
817 log_legal($curtime, 'xxx', $user_cur, "STAT:DESTROY_GAME(RECOVERY)", $plist);
819 $this->room_join_wakeup($user_cur, TRUE, -2);
820 $table_cur->table_token = "";
823 Bin5::unlock_data($sem);
825 } // if ($table_cur->player_n == PLAYERS_N) {
826 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
828 log_rd2("out new loop.");
830 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
831 $user_cur = $this->user[$i];
833 log_rd2("User: ".$user_cur->name." stat: ".$user_cur->stat." subst: ".$user_cur->subst);
835 if ($user_cur->sess == "")
838 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) {
839 // Auto logout dell'utente
840 log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime ".$curtime);
842 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
843 log_auth($user_cur->sess, "Autologout session.");
847 log_rd2("AUTO LOGOUT.");
848 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
849 $this->room_wakeup($user_cur);
850 else if ($user_cur->subst == 'standup')
851 $this->room_outstandup($user_cur);
853 log_rd2("LOGOUT FROM WHAT ???");
857 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
858 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
859 $this->room_wakeup($user_cur);
860 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
861 /* 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" */
862 $user_cur->comm[$user_cur->step % COMM_N] .= show_notify($mlang_brisk['tabtout_a'][$G_lang].(EXPIRE_TIME_SMAMMA/60.0).$mlang_brisk['tabtout_b'][$G_lang], 0, $mlang_brisk['btn_backstand'][$G_lang], 400, 100);
863 $user_cur->step_inc();
867 log_rd2("GARBAGE UPDATED!");
869 // externalized $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
870 Room::garbage_time_expire_set($curtime + GARBAGE_TIMEOUT);
876 function show_room($user_step, $user)
878 GLOBAL $G_lang, $mlang_brisk;
879 log_main("show_room: username: ".$user->name);
881 $ret = sprintf('gst.st = %d; ', $user_step);
883 if ($user->flags & USER_FLAG_ISOLAUTH) {
884 $ret .= 'list_set(\'isolation\', false, \''.$mlang_brisk['tit_onisol'][$G_lang].'\' ); ';
886 else if ($user->flags & USER_FLAG_LISTAUTH) {
887 $ret .= 'list_set(\'auth\', false, \''.$mlang_brisk['tit_onauth'][$G_lang].'\' ); ';
890 $ret .= 'list_set(\'all\', false, \'\' ); ';
893 if ($user->subst == 'standup')
894 $ret .= "tra.show(); ";
896 $ret .= "tra.hide(); ";
898 $ret .= sprintf('stat = "%s";', $user->stat);
900 $ret .= root_wellcome($user);
901 if ($user->flags & USER_FLAG_DBFAILED) {
902 $ret .= "gst.st = ".($user->step+1)."; ";
903 $ret .= show_notify($mlang_brisk['db_failed'][$G_lang], 0, $mlang_brisk['btn_close'][$G_lang], 400, 140);
906 $ret .= sprintf('subst = "%s";', $user->subst);
907 $ret .= $user->myname_innerHTML();
909 for ($i = 0 ; $i < TABLES_N ; $i++) {
911 $ret .= $this->table_content($user, $i);
912 // $ret .= table_act_content(($user->subst == 'standup'), $this->table[$i]->player_n, $i, $user->table,
913 // ($this->table[$i]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
914 $ret .= $this->table[$i]->act_content($user);
915 if ($this->table[$i]->wag_own != -1)
916 $ret .= sprintf('tra.add(%d, "%s: %s"); ', $i, $this->user[$this->table[$i]->wag_own]->name, $this->table[$i]->wag_com);
918 $ret .= sprintf('tra.rem(%d); ', $i);
920 $ret .= $this->standup_content($user);
926 function room_wakeup($user)
928 $table_idx = $user->table;
929 $table = $this->table[$table_idx];
931 log_main("WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
935 $from_table = ($user->stat == "table");
937 log_main("WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
939 for ($i = 0 ; $i < $table->player_n ; $i++) {
940 $user_cur = $this->user[$table->player[$i]];
941 log_main("PREIMPOST: INLOOP name: ".$user_cur->name);
943 if ($user->idx_get() != $table->player[$i]) {
944 $user_cur->stat_set("room");
945 $user_cur->subst = "sitdown";
946 $user_cur->laccwr = $curtime;
948 else if ($user->sess != "") {
949 $user_cur->stat_set("room");
950 $user_cur->subst = "standup";
951 $user_cur->laccwr = $curtime;
952 $user_cur->table = -1;
957 $user->stat_set("room");
958 $user->subst = "standup";
959 $user->laccwr = $curtime;
962 $remove_wagon = FALSE;
963 if($table->wag_own == $user->idx_get()) {
964 $table->wag_reset($curtime);
965 $remove_wagon = TRUE;
969 /* aggiorna l'array dei giocatori al tavolo. */
970 $table->user_rem($this, $user);
972 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
973 $user_cur = $this->user[$i];
974 if ($user_cur->sess == '' || $user_cur->stat != 'room')
977 // log_main("VALORI: name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
979 $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
980 if ($from_table && ($user_cur->table == $table_idx || $user->idx_get() == $i)) {
981 $ret .= 'gst.st_loc++; hstm.stop(); window.onunload = null; window.onbeforeunload = null; document.location.assign("index.php");|';
982 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
983 log_main("DOCUMENT.index.php: from table");
985 else if ($user_cur->stat == "room") {
986 log_main("DOCUMENT.index.php: from table");
988 $ret .= $this->table_content($user_cur, $table_idx);
989 $ret .= $this->standup_content($user_cur);
991 // $ret .= table_act_content(FALSE, 0, $table_idx, $user->table, FALSE);
992 $ret .= $table->act_content($user);
994 if ($user->idx_get() == $i) {
995 // set the new status
996 $ret .= 'subst = "standup"; tra.show(); ';
997 // clean the action buttons in other tables
998 for ($e = 0 ; $e < TABLES_N ; $e++) {
999 if ($this->table[$e]->player_n < PLAYERS_N) {
1000 // $ret .= table_act_content(TRUE, 0, $e, $user->table,
1001 // ($this->table[$e]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
1002 $ret .= $this->table[$e]->act_content($user);
1007 // $ret .= table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table,
1008 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1009 $ret .= $table->act_content($user_cur);
1012 log_wr("ROOM_WAKEUP: ".$ret);
1013 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1014 $user_cur->step_inc();
1018 function room_join_wakeup($user, $update_lacc = FALSE, $trans_delta)
1020 $table_idx = $user->table;
1021 $table = $this->table[$table_idx];
1023 log_main("JOIN_WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
1026 $user_wup = array();
1028 $user_tab = array();
1030 log_main("JOIN WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1032 for ($i = 0 ; $i < $table->player_n ; $i++) {
1033 $user_cur = $this->user[$table->player[$i]];
1034 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1035 if ($user_cur->sess != "") {
1036 if ($update_lacc == TRUE) {
1037 $user_cur->laccwr = $curtime;
1039 log_main("cur: ".$user_cur->name." subst: ".$user_cur->subst);
1040 if ($user_cur->subst == "shutdowned") {
1041 $user_cur->stat_set("room");
1042 $user_cur->subst = "sitdown";
1044 else if ($user_cur->subst == "shutdowner") {
1045 $user_cur->stat_set("room");
1046 $user_cur->subst = "standup";
1047 $user_cur->table = -1;
1048 $user_wup[$user_wup_n++] = $user_cur;
1050 $remove_wagon = FALSE;
1051 if($table->wag_own == $table->player[$i]) {
1052 $remove_wagon = TRUE;
1053 $table->wag_reset($curtime);
1056 $user_tab[$user_tab_n++] = $table->player[$i];
1060 for ($wup_idx = 0 ; $wup_idx < $user_wup_n ; $wup_idx++)
1061 $table->user_rem($this, $user_wup[$wup_idx]);
1063 /* aggiorna l'array dei giocatori al tavolo. */
1065 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1066 log_main("START LOOP");
1067 $user_cur = $this->user[$i];
1068 if ($user_cur->sess == '' || $user_cur->stat != 'room') {
1069 log_main("name: ".$user_cur->name."skip subst: ".$user_cur->subst);
1074 log_main("VALORI name: ".$user_cur->name." tab: ".$user_cur->table." taix: ".$table_idx);
1076 $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
1077 if ($user_cur->stat == "room") {
1078 log_main("DOCUMENT.index.php from table");
1080 $ret .= $this->table_content($user_cur, $table_idx);
1081 $ret .= $this->standup_content($user_cur);
1083 // $ret .= table_act_content(FALSE, 0, $table_idx, $user_cur->table,
1084 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1085 $ret .= $table->act_content($user_cur);
1088 for ($tab_idx = 0 ; $tab_idx < $user_tab_n ; $tab_idx++)
1089 if ($user_tab[$tab_idx] == $i)
1092 // for users that wakeup the room will be reconstructed by index_rd.php
1093 if ($tab_idx < $user_tab_n) {
1094 log_main("PRE show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1096 // ARRAY_POP DISABLED
1097 // if ($trans_delta == 0)
1098 // while (array_pop($user_cur->comm) != NULL);
1100 $user_cur->trans_step = $user_cur->step + 1 + $trans_delta;
1101 $user_cur->comm[$user_cur->step % COMM_N] = "";
1102 $user_cur->step_inc();
1103 $user_cur->comm[$user_cur->step % COMM_N] = $this->show_room(($user_cur->step + 1), $user_cur);
1104 $user_cur->step_inc();
1105 log_main("POST show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1109 log_main("JOIN_WAKEUP wup_idx ".$wup_idx." wup_n ".$user_wup_n);
1111 log_main("JOIN_WAKEUP more");
1112 // $ret .= table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table,
1113 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1114 $ret .= $table->act_content($user_cur);
1116 log_main("JOIN_WAKEUP end more");
1118 log_wr("ROOM_JOIN_WAKEUP: ".$ret);
1119 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1120 $user_cur->step_inc();
1124 function room_outstandup($user)
1126 $this->room_sitdown($user, -1);
1129 function table_update($user)
1131 log_main("table_update: pre - USER: ".$user->name);
1133 $table_idx = $user->table;
1135 if ($table_idx > -1)
1136 $table = $this->table[$table_idx];
1138 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1140 $user_cur = $this->user[$i];
1141 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1144 $ret = "gst.st = ".($user_cur->step+1)."; ";
1145 if ($table_idx > -1)
1146 $ret .= $this->table_content($user_cur, $table_idx);
1148 if ($user->idx_get() == $i) {
1149 $ret .= $user->myname_innerHTML();
1151 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1152 $user_cur->step_inc();
1155 log_main("table_update: post");
1158 function room_sitdown($user, $table_idx)
1160 log_main("room_sitdown ".($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
1164 if ($table_idx > -1 && $table_idx < TABLES_N) {
1165 $table = $this->table[$table_idx];
1168 if ($table->wag_own != -1 && $table->player_n == PLAYERS_N) {
1169 for ($i = 0 ; $i < TABLES_N ; $i++) {
1170 if ($table->wag_own == $table->player[$i]) {
1171 $train_app = sprintf("tra.rem(%d); ", $table_idx);
1172 $table->wag_reset(time());
1179 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1181 $user_cur = $this->user[$i];
1182 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1185 $ret = "gst.st = ".($user_cur->step+1)."; ".$train_app;
1186 if ($table_idx > -1)
1187 $ret .= $this->table_content($user_cur, $table_idx);
1188 $ret .= $this->standup_content($user_cur);
1190 if ($user->idx_get() == $i) {
1191 $ret .= 'subst = "sitdown"; tra.hide(); ';
1192 // clean the action buttons in other tables
1193 for ($e = 0 ; $e < TABLES_N ; $e++) {
1194 // $ret .= table_act_content(FALSE, 0, $e, $user_cur->table, FALSE);
1195 $ret .= $this->table[$e]->act_content($user_cur);
1198 else if ($table_idx > -1) {
1199 if ($table->player_n == PLAYERS_N) {
1200 // $ret .= table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table,
1201 /// ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1202 $ret .= $table->act_content($user_cur);
1205 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1206 $user_cur->step_inc();
1210 function chatt_send($user, $mesg)
1212 GLOBAL $G_base, $G_alarm_passwd, $mlang_brisk, $G_lang;
1216 $msg = substr($mesg, 6, 128);
1218 $dt = date("H:i ", $curtime);
1229 $is_normchat = FALSE;
1230 /* for old isolation management $is_ticker = FALSE; */
1231 $update_room = FALSE;
1233 if (strcmp($msg, "/tav") == 0 ||
1234 strncmp($msg, "/tav ", 5) == 0) {
1236 if ($user->stat != 'room' || $user->subst != 'sitdown') {
1237 /* MLANG: "<br>Per attivare il messaggio di segnalazione del tavolo occorre essere seduti.<br><br>", "<br>Il messaggio di segnalazione del tavolo è già attivato.<br><br>", "<br>Il messaggio di segnalazione del tavolo<br>è disattivato ancora per %d second%s.<br><br>" */
1238 $msg = $mlang_brisk['tickmust'][$G_lang];
1239 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1244 $table = $this->table[$user->table];
1246 if ($table->wag_own != -1) {
1247 // MLANG <br>Il messaggio di segnalazione del tavolo è già attivato.<br><br>
1248 $msg = $mlang_brisk['tickjust'][$G_lang];
1249 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1254 $dtime = $curtime - $table->wag_tout;
1255 if ($dtime < EXPIRE_TIME_WAG) {
1256 // MLANG - <br>Il messaggio di segnalazione del tavolo<br>è disattivato ancora per %d second%s.<br><br>
1257 $msg = sprintf($mlang_brisk['tickwait'][$G_lang],
1258 EXPIRE_TIME_WAG - $dtime, (EXPIRE_TIME_WAG - $dtime == 1 ? ($G_lang == 'en' ? "" : "o") : ($G_lang == 'en' ? "s" : "i")));
1259 $to_user = show_notify($msg, 0, $mlang_brisk['btn_close'][$G_lang] , 400, 100);
1264 $msg = substr($msg, 5);
1266 $table->wag_set($user->idx_get(), $msg);
1267 $to_user = sprintf('tra.add(%d, "%s");', $user->table, xcape(sprintf("%s: %s", $user->name, $msg)));
1268 $to_room = $to_user;
1269 /* for old isolation management $is_ticker = TRUE; */
1271 } // /tav chat command
1273 else if (strncmp($msg, "/alarm ", 7) == 0) {
1274 if (strncmp($msg, "/alarm to ", 10) == 0) {
1275 $sp_pos = strpos($msg, " ", 10);
1276 $target = substr($msg, 10, $sp_pos - 10);
1277 $alarm_check = "/alarm to ".$target." ".$G_alarm_passwd." ";
1281 $alarm_check = "/alarm ".$G_alarm_passwd." ";
1284 if (strncmp($msg, $alarm_check, strlen($alarm_check)) != 0) {
1285 /* MLANG: "<br>La password digitata non è corretta.<br><br>" */
1286 $msg = $mlang_brisk['alarpass'][$G_lang];
1287 $to_user = show_notify($msg, 0, $mlang_brisk['btn_close'][$G_lang], 400, 100);
1292 /* MLANG: "Alarm <b>%s</b> inviato a <b>%s</b>." */
1293 $prestr = sprintf($mlang_brisk['alarret'][$G_lang], xcape(substr($msg, strlen($alarm_check))),
1294 ($target == "" ? $mlang_brisk['tit_all'][$G_lang] : xcape($target)) );
1295 $to_user = sprintf('chatt_sub("%s", [2, "%s"],%s);',
1296 $dt, NICKSERV, $prestr);
1298 $msg = sprintf("<br><b>%s<br><br>%s</b><br><br>",
1299 $dt.NICKSERV, xcape(substr($msg, strlen($alarm_check))));
1300 /* MLANG: "chiudi" */
1301 $to_all = show_notify($msg, 0, $mlang_brisk['btn_close'][$G_lang], 400, 120);
1303 } // /alarm chat command
1304 else if (strncmp($msg, "/listen ", 8) == 0) {
1305 $arg = substr($msg, 8);
1307 if (strcasecmp($arg, "isolation") == 0) {
1309 if ($user->stat == 'room' && $user->subst == 'sitdown' &&
1310 $user->table >= TABLES_AUTH_N) {
1311 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['listmust'][$G_lang]);
1315 $user->flags &= ~USER_FLAG_MAP_AUTH;
1316 $user->flags |= USER_FLAG_ISOLAUTH;
1317 $to_user = 'list_set(\'isolation\', true, \''.$mlang_brisk['tit_onisol'][$G_lang].'\'); ';
1320 else if (strcasecmp($arg, "auth") == 0) {
1321 $flags_old = $user->flags;
1322 $user->flags &= ~USER_FLAG_MAP_AUTH;
1323 $user->flags |= USER_FLAG_LISTAUTH;
1324 $to_user = 'list_set(\'auth\', true, \''.$mlang_brisk['tit_onauth'][$G_lang].'\'); ';
1327 $flags_old = $user->flags;
1328 $user->flags &= ~USER_FLAG_MAP_AUTH;
1329 $to_user = 'list_set(\'all\', true, \'\'); ';
1332 // if from isolation redraw standup area
1333 if (($flags_old ^ $user->flags) & USER_FLAG_ISOLAUTH) {
1334 $to_user .= 'standup_data_old = null; '.$this->standup_content($user);
1338 else if (strcmp($msg, "/authreq") == 0) {
1339 if ($user->flags & USER_FLAG_AUTH) {
1340 $to_user = sprintf('authbox(300,200);');
1343 /* 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à in uso." */
1344 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['authmust'][$G_lang]);
1347 else if (strncmp($msg, "/mesgtoadm", 8) == 0) {
1348 if ($user->flags & USER_FLAG_AUTH) {
1349 $to_user = sprintf('mesgtoadmbox(500,300);');
1352 /* MLANG: "<b>Per inviare un messaggio devi essere autenticato.</b>" */
1353 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['mesgmust'][$G_lang]);
1356 else if (strncmp($msg, "/nick ", 6) == 0) {
1357 log_main("chatt_send BEGIN");
1360 if (($name_new = validate_name(substr($msg, 6))) == FALSE) {
1361 $to_user = sprintf('chatt_sub("%s", [2,"%s"],"%s");', $dt, NICKSERV, $mlang_brisk['nickmust'][$G_lang]);
1365 $msg = "COMMAND ".$msg;
1366 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1367 $user_cur = $this->user[$i];
1369 if ($user_cur->sess == '')
1371 if (strcasecmp($user_cur->name,$name_new) == 0)
1374 if ($i < MAX_PLAYERS) {
1375 $prestr = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
1376 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $prestr);
1380 /* MLANG: "<b>Non puoi cambiare nick a un tavolo per soli autenticati.</b>", "Il nickname <b>\'%s\'</b> è già registrato, <b>se il suo proprietario si autentificherà verrai rinominato d\'ufficio come ghost<i>N</i>.</b>" */
1381 if ($user->flags & USER_FLAG_AUTH) {
1382 if (strcasecmp($user->name,$name_new) != 0) {
1383 if (( ($user->flags & USER_FLAG_MAP_AUTH) != USER_FLAG_ISOLAUTH) &&
1384 ($user->subst == 'standup' ||
1385 ($user->subst != 'standup' && $this->table[$user->table]->auth_only == FALSE)
1388 $user->flags &= ~(USER_FLAG_AUTH | USER_FLAG_TY_ALL); // Remove auth if name changed
1389 for ($i = 0 ; $i < TABLES_N ; $i++) {
1390 $to_user .= $this->table[$i]->act_content($user);
1394 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['authchan'][$G_lang]);
1399 $user->name = $name_new; // OK - nick changed
1400 /* se nome gia' in uso, segnala cosa potrebbe capitare */
1401 if (($user->flags & USER_FLAG_AUTH) == 0) {
1402 if (($bdb = BriskDB::create()) != FALSE) {
1404 /* MLANG: "Il nickname <b>\'%s\'</b> è già registrato, <b>se il suo proprietario si autentificherà verrai rinominato d\'ufficio come ghost<i>N</i>.</b>" */
1405 if ($bdb->login_exists($name_new)) {
1406 $prestr = sprintf($mlang_brisk['nickjust'][$G_lang], xcape($name_new));
1407 $to_user .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $prestr);
1412 log_main("chatt_send start set");
1414 $update_room = TRUE;
1416 } // nick chat command
1418 else if (strncmp($msg, "/st ", 4) == 0) {
1419 log_main("chatt_send BEGIN");
1422 $st_str = substr($msg, 4);
1424 if (strcasecmp($st_str, "normale") == 0) {
1425 $st = USER_FLAG_S_NORM;
1427 else if (strcasecmp($st_str, "pausa") == 0) {
1428 $st = USER_FLAG_S_PAU;
1430 else if (strcasecmp($st_str, "fuori") == 0) {
1431 $st = USER_FLAG_S_OUT;
1433 else if (strcasecmp($st_str, "cane") == 0) {
1434 $st = USER_FLAG_S_DOG;
1436 else if (strcasecmp($st_str, "cibo") == 0) {
1437 $st = USER_FLAG_S_EAT;
1439 else if (strcasecmp($st_str, "lavoro") == 0) {
1440 $st = USER_FLAG_S_WRK;
1442 else if (strcasecmp($st_str, "sigaretta") == 0) {
1443 $st = USER_FLAG_S_SMK;
1445 else if (strcasecmp($st_str, "presente") == 0) {
1446 $st = USER_FLAG_S_EYE;
1448 else if (strcasecmp($st_str, "coniglio") == 0) {
1449 $st = USER_FLAG_S_RABB;
1451 else if (strcasecmp($st_str, "calcio") == 0) {
1452 $st = USER_FLAG_S_SOCC;
1454 else if (strcasecmp($st_str, "pupo") == 0) {
1455 $st = USER_FLAG_S_BABY;
1457 else if (strcasecmp($st_str, "pulizie") == 0) {
1458 $st = USER_FLAG_S_MOP;
1461 /* MLANG: "Questo stato non esiste." */
1462 $to_user = sprintf('chatt_sub("%s", [2,"%s"],"%s");', $dt, NICKSERV, $mlang_brisk['statunkn'][$G_lang]);
1466 log_main("chatt_send start set");
1467 if (($user->flags & USER_FLAG_S_ALL) != $st) {
1468 $update_room = TRUE;
1469 $user->flags = ($user->flags & ~USER_FLAG_S_ALL) | $st;
1472 } // nick chat command
1474 else { // normal chat line
1475 $is_normchat = TRUE;
1476 if ($curtime < ($user->chat_ban + $user->chat_dlt)) {
1478 $user->chat_dlt = $user->chat_dlt * 2;
1479 if ($user->chat_dlt > 120)
1480 $user->chat_dlt = 120;
1482 else if ($user->chat_lst == $msg)
1484 else if ($curtime - $user->chattime[($user->chat_cur + 1) % CHAT_N] < CHAT_ILL_TIME) {
1485 $user->chat_ban = $curtime;
1486 $user->chat_dlt = 5;
1490 $user->chat_ban = 0;
1491 $user->chat_dlt = 0;
1495 $to_user = sprintf('chatt_sub("%s", [%d, "%s"],"%s");', $dt, $user->flags, xcape($user->name), xcape("== chat ban =="));
1498 $to_user = sprintf('chatt_sub("%s", [%d, "%s"],"%s");', $dt, $user->flags, xcape($user->name), xcape($msg));
1499 // temporary silentiation for troll (will became array check)
1500 // if (strcasecmp($user->name,'JackRokka') != 0 && $user->sess != '47ea653f602e8')
1501 $to_room = $to_user;
1504 log_legal($curtime, 'xxx', $user,
1505 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$msg);
1507 $user->chat_lst = "$msg";
1508 $user->chattime[$user->chat_cur % CHAT_N] = $curtime;
1518 // Output to clients
1521 if ($to_user != FALSE) {
1522 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1523 $user->comm[$user->step % COMM_N] .= $to_user;
1527 if ($to_room != FALSE) {
1528 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1529 $user_cur = $this->user[$i];
1530 if ($target != "" && $user_cur->name != $target)
1532 if ($user_cur->sess == '' || $user_cur->stat == 'table' || $user->idx_get() == $i)
1535 if ($is_normchat == TRUE) {
1536 // use MAP_AUTH to check if auth or isolation
1537 if ($user_cur->flags & USER_FLAG_MAP_AUTH) {
1538 if (($user->flags & USER_FLAG_AUTH) == 0) {
1544 else if ($is_ticker) {
1545 if (($user_cur->flags & USER_FLAG_MAP_AUTH) == USER_FLAG_ISOLAUTH) {
1546 if ($user->table >= TABLES_AUTH_N)
1551 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1552 $user_cur->comm[$user_cur->step % COMM_N] .= $to_room;
1553 $user_cur->step_inc();
1558 // FIXME BRISK4: include for each kind of table
1559 require_once("${G_base}briskin5/Obj/briskin5.phh");
1560 // Before all align times with table timeout
1561 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1562 $table_cur = $this->table[$table_idx];
1563 // if the table is complete and exists its shared mem we get the info about users lacc
1565 if ($table_cur->player_n == PLAYERS_N) {
1566 log_main("PLAYERS == N TABLE ".$table_idx);
1568 if (($sem = Bin5::lock_data(TRUE, $table_idx)) != FALSE) {
1569 log_main("bin5 lock data success");
1571 $no_recovery = FALSE;
1572 if (($bri = Bin5::load_data($table_idx)) != FALSE) {
1573 if ($table_cur->table_token != $bri->table_token) {
1574 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
1579 if ($bri != FALSE) {
1580 $bri_table = $bri->table[0];
1581 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
1582 // stat must be "table" by definition
1583 $bri_user = $bri->user[$i];
1585 if ($target != "" && $bri_user->name != $target)
1587 log_main("writa: ".$user_mesg);
1588 $bri_user->comm[$bri_user->step % COMM_N] = "gst.st = ".($bri_user->step+1)."; ";
1589 $bri_user->comm[$bri_user->step % COMM_N] .= $to_tabl;
1590 $bri_user->step_inc();
1592 Bin5::save_data($bri);
1594 Bin5::unlock_data($sem);
1596 } // if ($table_cur->player_n == PLAYERS_N) {
1597 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1598 } // if ($to_tabl == true ...
1601 if ($user->stat == 'room' && $user->subst == 'standup') {
1602 $this->standup_update($user);
1604 else if ($user->stat == 'room' && $user->subst == 'sitdown') {
1605 log_main("chatt_send pre table update");
1606 $this->table_update($user);
1607 log_main("chatt_send post table update");
1609 } // if ($update_room ...
1612 } // function chatt_send( ...
1614 function get_user($sess, &$idx)
1618 if (validate_sess($sess)) {
1619 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1620 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1623 $ret = $this->user[$i];
1627 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1628 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
1629 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1632 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1639 * function add_user(&$room, &$sess, &$idx, $name, $pass, $ip)
1642 * if ($idx > -1 && ret == FALSE) => duplicated nick
1643 * if ($idx == -2 && ret == FALSE) => invalid name
1644 * if ($idx == -3 && ret == FALSE) => wrong password
1645 * if ($idx == -1 && ret == FALSE) => no space left
1646 * if ($idx == 0 && ret == user) => SUCCESS
1647 * if ($idx == -$idx && ret == user) => SUCCESS (but the login exists in the auth db)
1650 function add_user(&$sess, &$idx, $name, $pass, $ip)
1652 GLOBAL $G_base, $CO_list;
1656 $authenticate = FALSE;
1658 $login_exists = FALSE;
1660 $ghost_auth = FALSE;
1665 if (($name_new = validate_name($name)) == FALSE) {
1670 log_auth("XXX", sprintf("ARRIVA: [%s] pass:[%s]", $sess, ($pass == FALSE ? "FALSE" : $pass)));
1671 if (validate_sess($sess) == FALSE)
1674 /* if pass != FALSE verify the login with pass */
1675 log_auth("XXX", "auth1");
1677 if (($bdb = BriskDB::create()) != FALSE) {
1679 if ($pass != FALSE) { // TODO: here add a method to $bdb to check if the db is available.
1680 log_auth("XXX", "auth2");
1681 $authenticate = $bdb->login_verify($name_new, $pass, $code);
1682 log_auth("XXX", "authenticate: ".($authenticate != FALSE ? "TRUE" : "FALSE"));
1684 if ($authenticate != FALSE) {
1685 $user_type = $authenticate->type_get();
1693 $login_exists = $bdb->login_exists($name_new);
1697 // if db is down, send a warning and verify only current users
1698 // no actions at this moment
1700 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1702 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1706 if ($idfree == -1 && strcmp($this->user[$i]->sess, "") == 0) {
1708 continue; // NOTE: CHECK IT !!
1710 if (strcasecmp($this->user[$i]->name, $name_new) == 0) {
1711 if ($authenticate != FALSE) {
1713 $ghost_auth = ($this->user[$i]->flags & USER_FLAG_AUTH);
1724 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1726 /* there is another user logged with your account and you and him have authenticated => new user
1727 get the session of the old user */
1728 if ($ghost > -1 && $ghost_auth && ($authenticate != FALSE)) {
1731 $ghost_user = $this->user[$ghost];
1733 $ghost_user->step_inc();
1736 $ghost_user->sess = $sess;
1739 $ghost_user->sess = $sess;
1742 // If user at the table we need to update the table data too
1743 $table_idx = $ghost_user->table;
1744 if ($ghost_user->stat == "table" && $this->table[$table_idx]->player_n == PLAYERS_N) {
1745 // FIXME BRISK4: include for each kind of table
1746 require_once("${G_base}briskin5/Obj/briskin5.phh");
1747 if (($brisem = Bin5::lock_data(TRUE, $table_idx)) != FALSE) {
1748 if (($bri = Bin5::load_data($table_idx)) != FALSE) {
1749 if ($bri->the_end != TRUE) {
1750 $bri->user[$ghost_user->table_pos]->step_inc();
1751 $bri->user[$ghost_user->table_pos]->sess = $sess;
1752 Bin5::save_data($bri);
1755 Bin5::unlock_data($brisem);
1760 return ($this->user[$ghost]);
1762 else if ($idx != -1 && $i == MAX_PLAYERS) {
1767 $this->user[$idx]->sess = $sess;
1770 $this->user[$idx]->sess = $sess;
1772 $this->user[$idx]->name = $name_new; // OK - add new user
1773 $this->user[$idx]->stat_set("room");
1774 $this->user[$idx]->step_set(0);
1775 while (array_pop($this->user[$idx]->comm) != NULL);
1776 $this->user[$idx]->subst = "standup";
1777 $this->user[$idx]->lacc = $curtime;
1778 $this->user[$idx]->laccwr = $curtime;
1779 $this->user[$idx]->bantime = 0;
1780 $this->user[$idx]->ip = $ip;
1782 $this->user[$idx]->flags = $user_type;
1783 $this->user[$idx]->flags |= ($authenticate != FALSE ? USER_FLAG_AUTH : 0x00);
1784 $this->user[$idx]->flags |= ( ($pass != FALSE && $bdb == FALSE) ? USER_FLAG_DBFAILED : 0x00);
1785 log_auth("XXX", sprintf("FLAGS: [%x]", $this->user[$idx]->flags));
1788 if ($authenticate != FALSE) {
1789 $this->user[$idx]->code = $authenticate->code_get();
1790 $this->user[$idx]->flags |= USER_FLAG_LISTAUTH;
1792 if (isset($CO_list)) {
1793 if (strcmp($CO_list, "auth") == 0) {
1794 $this->user[$idx]->flags &= ~USER_FLAG_MAP_AUTH;
1795 $this->user[$idx]->flags |= USER_FLAG_LISTAUTH;
1797 if (strcmp($CO_list, "isolation") == 0) {
1798 $this->user[$idx]->flags &= ~USER_FLAG_MAP_AUTH;
1799 $this->user[$idx]->flags |= USER_FLAG_ISOLAUTH;
1802 $this->user[$idx]->flags &= ~USER_FLAG_MAP_AUTH;
1808 log_main("ghost: rename!");
1809 $ghost_user = $this->user[$ghost];
1811 if ($ghost_auth == FALSE) {
1812 for ($sfx = 1 ; $sfx <= MAX_PLAYERS ; $sfx++) {
1813 $ghostname = 'ghost'.$sfx;
1814 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1815 if (strcmp("", $this->user[$i]->sess) == 0)
1818 if (strcasecmp($this->user[$i]->name, $ghostname) == 0) {
1823 if ($ghostname != '')
1827 $ghost_user->name = $ghostname;
1829 if ($ghost_user->stat == 'room' && $ghost_user->subst == 'standup') {
1830 $this->standup_update($ghost_user);
1833 log_main("chatt_send pre table update");
1834 $this->table_update($ghost_user);
1835 log_main("chatt_send post table update");
1837 } // if ($ghost_auth == FALSE
1839 // FIXME: cacciare il vecchio utente room && table (if needed)
1840 $ghost_user->the_end = TRUE;
1841 $ghost_user->lacc = 0;
1842 $this->garbage_manager(TRUE);
1844 } // if ($ghost > -1) {
1849 log_main(sprintf("TROVATO LIBERO A [%d] sess [%s] name [%s] count [%d] name [%s] code [%s]", $idx, $sess, $name_new, count($this->user),$this->user[$real_idx]->name, $this->user[$real_idx]->code));
1851 $ret = $this->user[$real_idx];
1858 function standup_update($user)
1860 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1861 $user_cur = $this->user[$i];
1862 if ($user_cur->sess == '')
1865 log_main("STANDUP START: ".$user_cur->stat);
1867 if ($user_cur->stat == 'room') {
1868 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".$this->standup_content($user_cur);
1869 if ($user->idx_get() == $i) {
1870 $user_cur->comm[$user_cur->step % COMM_N] .= $user->myname_innerHTML();
1872 log_main("FROM STANDUP: NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
1874 $user_cur->step_inc();
1880 static function create()
1888 function load_data()
1893 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
1894 log_main("ftok failed");
1898 if (($shm_sz = sharedmem_sz($tok)) == -1) {
1899 log_main("shmop_open failed");
1903 $shm_sz = SHM_DIMS_MIN;
1905 if ($shm = shm_attach($tok, $shm_sz)) {
1906 $room = @shm_get_var($shm, $tok); // CHECKED BELOW
1908 log_only("bri == ".($room == FALSE ? "FALSE" : "TRUE")." bri === ".($room === FALSE ? "FALSE" : "TRUE")." bri isset ".(isset($room) ? "TRUE" : "FALSE"));
1910 log_only("bri count ".count($room));
1912 if ($room == FALSE) {
1913 log_only("INIT MAIN DATA");
1916 $room = Room::create();
1918 log_shme("Room::create");
1920 if (Room::save_data($room) == FALSE)
1925 $room->shm_sz = $shm_sz;
1929 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1930 if (($room->user[$i] = User::load_data($i, FALSE)) == FALSE) {
1931 log_crit("User::load_data failed");
1935 if ($i < MAX_PLAYERS) {
1941 // SHSPLIT: load users from the shared memory
1950 function save_data_orig($room)
1958 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1)
1961 while ($room->shm_sz < SHM_DIMS_MAX) {
1962 if (($shm = shm_attach($tok, $room->shm_sz)) == FALSE)
1965 // log_only("PUT_VAR DI ".strlen(serialize($room)));
1966 if (@shm_put_var($shm, $tok, $room) != FALSE) {
1970 if (shm_remove($shm) === FALSE) {
1971 log_only("REMOVE FALLITA");
1975 $room->shm_sz += SHM_DIMS_DLT;
1985 function save_data($room)
1992 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1)
1995 // SHSPLIT: before save the $room you must save users,
1996 // detach from main struct and (then) reattach
1997 $user_park = array();
1998 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1999 $user_park[$i] = $room->user[$i];
2000 $room->user[$i] = FALSE;
2003 while ($room->shm_sz < SHM_DIMS_MAX) {
2004 if (($shm = shm_attach($tok, $room->shm_sz)) == FALSE)
2007 // log_only("PUT_VAR DI ".strlen(serialize($room)));
2008 if (@shm_put_var($shm, $tok, $room) != FALSE) {
2009 log_shme("Room::save_data");
2013 if (shm_remove($shm) === FALSE) {
2014 log_only("REMOVE FALLITA");
2018 $room->shm_sz += SHM_DIMS_DLT;
2024 // SHSPLIT: reattach users to the room class
2025 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
2026 User::save_data($user_park[$i], $i);
2027 $room->user[$i] = $user_park[$i];
2029 log_load("FINISH: ".($ret == TRUE ? "TRUE" : "FALSE"));
2034 static function lock_data($is_exclusive)
2036 if (($res = file_lock(FTOK_PATH."/main", $is_exclusive)) != FALSE) {
2037 self::$delta_t = microtime(TRUE);
2038 log_lock("LOCK room [".self::$delta_t."]");
2046 static function unlock_data($res)
2050 log_lock("UNLOCK room [".(microtime(TRUE) - (self::$delta_t))."]");
2056 function standup_content($user)
2061 if ($user->stat != 'room')
2064 for ($i = 0 , $ct = 0 ; $ct < 4 && $i < MAX_PLAYERS ; $i++) {
2065 if ($this->user[$i]->sess == "" || $this->user[$i]->stat != "room" || $this->user[$i]->name == "")
2070 // $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
2072 $content = ' j_stand_cont( [ ';
2074 for ($i = 0 , $ct = 0 ; $i < MAX_PLAYERS ; $i++) {
2075 if ($this->user[$i]->sess == "" || $this->user[$i]->stat != "room" || $this->user[$i]->name == "")
2078 $flags = $this->user[$i]->flags;
2080 if ($this->user[$i]->subst == "standup") {
2081 if ($user->idx_get() == $i) {
2085 $content .= sprintf('%s[ %d, "%s" ]',($ct > 0 ? ', ' : ''), $flags, xcape($this->user[$i]->name));
2094 function table_content($user, $table_idx)
2100 // Si possono usare i dati nella classe table
2103 $sess = $user->sess;
2104 $table = $this->table[$table_idx];
2106 if ($user->stat != 'room')
2110 for ($i = 0 ; $i < $table->player_n ; $i++) {
2111 $user_cur = $this->user[$table->player[$i]];
2113 $flags = $user_cur->flags;
2115 if ($user->idx_get() == $table->player[$i])
2118 log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
2120 $content .= sprintf('%s[ %d, "%s" ]',($i == 0 ? '' : ', '), $flags, xcape($user_cur->name));
2125 $ret .= sprintf('j_tab_cont(%d, %s);', $table_idx, $content);
2130 static function garbage_time_is_expired($tm)
2135 if (file_exists(PROXY_PATH) == FALSE)
2136 mkdir(PROXY_PATH, 0775, TRUE);
2137 if (($fp = @fopen(PROXY_PATH."/garbage_time.expired", 'rb')) == FALSE)
2139 if (($s = fread($fp, 4)) == FALSE)
2141 if (mb_strlen($s, "ASCII") != 4)
2143 $arr = unpack('Le', $s);
2144 if ($arr['e'] > $tm)
2151 log_rd2("END: return ".($ret ? "TRUE" : "FALSE"));
2156 static function garbage_time_expire_set($tm)
2159 if (file_exists(PROXY_PATH) == FALSE)
2160 mkdir(PROXY_PATH, 0775, TRUE);
2161 if (($fp = @fopen(PROXY_PATH."/garbage_time.expired", 'wb')) == FALSE)
2163 fwrite($fp, pack("L",$tm));
2172 function request_mgr(&$s_a_p, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
2174 printf("NEW_SOCKET (root): %d\n", intval($new_socket));
2178 case SITE_PREFIX."index.php":
2180 index_main($this, $header_out, $addr, $get, $post, $cookie);
2181 $content = ob_get_contents();
2184 $s_a_p->pgflush_try_add($new_socket, 20, $header_out, $content);
2188 case SITE_PREFIX."index_wr.php":
2190 index_wr_main($this, $addr, $get, $post, $cookie);
2191 $content = ob_get_contents();
2194 $s_a_p->pgflush_try_add($new_socket, 20, $header_out, $content);
2198 case SITE_PREFIX."index_rd_ifra.php":
2200 if (!isset($cookie['sess'])
2201 || (($user = $this->get_user($cookie['sess'], $idx)) == FALSE)) {
2202 $content = User::stream_fini(TRUE);
2204 $s_a_p->pgflush_try_add($new_socket, 20, $header_out, $content);
2209 // close a previous opened index_read_ifra socket, if exists
2210 if (($prev = $user->rd_socket_get()) != NULL) {
2211 $s_a_p->socks_unset($user->rd_socket_get());
2212 fclose($user->rd_socket_get());
2213 printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
2214 $user->rd_socket_set(NULL);
2218 $user->stream_init($header_out, $content, $get, $post, $cookie);
2220 $response = headers_render($header_out, -1).chunked_content($content);
2221 $response_l = mb_strlen($response, "ASCII");
2223 $wret = @fwrite($new_socket, $response, $response_l);
2224 if ($wret < $response_l) {
2225 printf("TROUBLES WITH FWRITE: %d\n", $wret);
2226 $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
2229 $user->rd_cache_set("");
2231 fflush($new_socket);
2234 $s_a_p->socks_set($new_socket, $user);
2235 $user->rd_socket_set($new_socket);
2236 printf(" - qui ci siamo - ");
2249 function make_seed()
2251 list($usec, $sec) = explode(' ', microtime());
2252 return (float) $sec + ((float) $usec * 100000);
2255 function btrace_line($ar)
2257 GLOBAL $G_btrace_pref_sub;
2260 for ($i = 0 ; $i < count($ar) ; $i++) {
2261 $with_class = isset($ar[$i]['class']);
2262 $with_file = isset($ar[$i]['file']);
2263 $ret .= sprintf("%s%s%s (%s:%d)", ($i == 0 ? "" : ", "),
2264 ($with_class ? $ar[$i]['class'].$ar[$i]['type'] : ""),
2265 $ar[$i]['function'], ($with_file ? str_replace($G_btrace_pref_sub, "", $ar[$i]['file']) : ""),
2266 ($with_file ? $ar[$i]['line'] : ""));
2272 function trace_ftok($id, $add)
2274 // NOTE: without space to use sed to substitute "= @ftok(" with "= @ftok("
2275 $tok=@ftok($id, $add);
2277 log_shme($tok.": ".$id." + ".$add);
2282 function log_mop($step, $log)
2284 GLOBAL $sess, $PHP_SELF;
2286 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LMOP) == 0)
2289 if (isset($sess) == FALSE)
2294 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LMOP) == 0)
2297 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2298 $btrace = btrace_line(debug_backtrace());
2301 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2302 fwrite($fp, sprintf("LMOP: [%f] [%05d] [%s] [%s]\n", gettimeofday(TRUE), $step, $log, $btrace));
2308 function log_only2($log)
2310 GLOBAL $sess, $PHP_SELF;
2312 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONL2) == 0)
2315 if (isset($sess) == FALSE)
2320 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONL2) == 0)
2323 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2324 $btrace = btrace_line(debug_backtrace());
2327 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2328 fwrite($fp, sprintf("ONL2: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2333 function log_crit($log)
2335 GLOBAL $sess, $PHP_SELF;
2337 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_CRIT) == 0)
2340 if (isset($sess) == FALSE)
2345 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_CRIT) == 0)
2348 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2349 $btrace = btrace_line(debug_backtrace());
2352 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2353 fwrite($fp, sprintf("CRIT: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2358 function log_only($log)
2360 GLOBAL $sess, $PHP_SELF;
2362 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONLY) == 0)
2365 if (isset($sess) == FALSE)
2370 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONLY) == 0)
2373 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2374 $btrace = btrace_line(debug_backtrace());
2377 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2378 fwrite($fp, sprintf("ONLY: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2383 function log_main($log)
2385 GLOBAL $sess, $PHP_SELF;
2387 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_MAIN) == 0)
2390 if (isset($sess) == FALSE)
2395 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_MAIN) == 0)
2398 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2399 $btrace = btrace_line(debug_backtrace());
2402 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2403 fwrite($fp, sprintf("MAIN: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2408 function log_rd($log)
2410 GLOBAL $sess, $PHP_SELF;
2412 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_READ) == 0)
2415 if (isset($sess) == FALSE)
2420 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_READ) == 0)
2423 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2424 $btrace = btrace_line(debug_backtrace());
2427 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2428 fwrite($fp, sprintf("READ: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2433 function log_rd2($log)
2435 GLOBAL $sess, $PHP_SELF;
2437 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_REA2) == 0)
2440 if (isset($sess) == FALSE)
2445 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_REA2) == 0)
2448 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2449 $btrace = btrace_line(debug_backtrace());
2453 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2454 fwrite($fp, sprintf("REA2: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2459 function log_send($log)
2461 GLOBAL $sess, $PHP_SELF;
2463 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SEND) == 0)
2466 if (isset($sess) == FALSE)
2471 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SEND) == 0)
2474 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2475 $btrace = btrace_line(debug_backtrace());
2478 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2479 fwrite($fp, sprintf("SEND: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2484 function log_lock($log)
2486 GLOBAL $sess, $PHP_SELF;
2488 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOCK) == 0)
2491 if (isset($sess) == FALSE)
2496 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOCK) == 0)
2499 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2500 $btrace = btrace_line(debug_backtrace());
2503 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2504 fwrite($fp, sprintf("LOCK: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2509 function log_wr($log)
2511 GLOBAL $sess, $PHP_SELF;
2513 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_WRIT) == 0)
2516 if (isset($sess) == FALSE)
2521 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_WRIT) == 0)
2524 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2525 $btrace = btrace_line(debug_backtrace());
2528 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2529 fwrite($fp, sprintf("WRIT: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2534 function log_load($log)
2536 GLOBAL $sess, $PHP_SELF;
2538 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOAD) == 0)
2541 if (isset($sess) == FALSE)
2546 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOAD) == 0)
2549 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2550 $btrace = btrace_line(debug_backtrace());
2553 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2554 fwrite($fp, sprintf("LOAD: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2559 function log_auth($sess, $log)
2563 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_AUTH) == 0)
2566 if (( (BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_AUTH) == 0)
2569 if ((BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2570 $btrace = btrace_line(debug_backtrace());
2573 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2574 fwrite($fp, sprintf("LOAD: [%s] [%d] [%s] [%s]\n", $sess, time(), $log, $btrace));
2579 function log_shme($log)
2581 GLOBAL $sess, $PHP_SELF;
2583 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SHME) == 0)
2586 if (isset($sess) == FALSE)
2591 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SHME) == 0)
2594 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2595 $btrace = btrace_line(debug_backtrace());
2598 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2599 fwrite($fp, sprintf("SHME: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2606 // function log_legal($curtime, $sess, $name, $where, $mesg)
2607 function log_legal($curtime, $addr, $user, $where, $mesg)
2610 if (($fp = @fopen(LEGAL_PATH."/legal.log", 'a')) != FALSE) {
2611 /* Unix time | session | nickname | IP | where was | mesg */
2612 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
2613 ($user->flags & USER_FLAG_AUTH ? 'A' : 'N'),
2614 $user->name, $addr, $where , $mesg));
2619 function table_act_content($isstanding, $sitted, $table, $cur_table, $allowed)
2624 if ($sitted < PLAYERS_N) {
2632 if ($table == $cur_table)
2639 $ret = sprintf('j_tab_act_cont(%d, \'%s\');', $table, $act);
2644 function show_notify($text, $tout, $butt, $w, $h)
2646 log_main("SHOW_NOTIFY: ".$text);
2647 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
2650 function show_notify_ex($text, $tout, $butt, $w, $h, $is_opaque, $block_time)
2652 log_main("SHOW_NOTIFY OPAQUE: ".$text);
2653 return sprintf('var noti = new notify_ex(gst,"%s",%d,"%s",%d,%d, %s, %d);', $text, $tout, $butt, $w, $h, ($is_opaque ? "true" : "false"), $block_time);
2657 function root_wellcome($user)
2659 GLOBAL $root_wellarr, $G_lang;
2663 $dt = date("H:i ", $curtime);
2665 for ($i = 0 ; $i < count($root_wellarr[$G_lang]) ; $i++)
2666 $ret .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, str_replace('"', '\"', $root_wellarr[$G_lang][$i]));
2673 function validate_sess($sess)
2675 if (strlen($sess) == SESS_LEN)
2681 function validate_name($name)
2683 $name_new = str_replace(' ', '_', substr(trim($name),0,12));
2685 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
2687 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
2694 function playsound($filename)
2696 return (sprintf('playsound("flasou", "%s");', $filename));
2699 function secstoword($secs)
2705 $mins = floor($secs / 60);
2707 if ($G_lang == 'en') {
2709 $ret = sprintf("%d minute%s%s", $mins, ($mins > 1 ? "s" : ""), ($secs > 0 ? " and " : ""));
2712 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "s" : ""));
2716 $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
2719 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));
2724 function sharedmem_sz($tok)
2726 if (($shm_id = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
2727 log_main("shmop_open failed");
2730 $shm_sz = shmop_size($shm_id);
2731 shmop_close($shm_id);
2733 // log_main("shm_sz: ".$shm_sz." SHM_DIMS: ".SHM_DIMS);
2740 static function lock_data($is_exclusive)
2742 if (($res = file_lock(FTOK_PATH."/warrant", $is_exclusive)) != FALSE) {
2743 self::$delta_t = microtime(TRUE);
2744 log_lock("LOCK warrant [".self::$delta_t."]");
2752 static function unlock_data($res)
2756 log_lock("UNLOCK warrant [".(microtime(TRUE) - (self::$delta_t))."]");
2765 static function lock_data($is_exclusive)
2767 if (($res = file_lock(FTOK_PATH."/poll", $is_exclusive)) != FALSE) {
2768 self::$delta_t = microtime(TRUE);
2769 log_lock("LOCK poll [".self::$delta_t."]");
2777 static function unlock_data($res)
2781 log_lock("UNLOCK poll [".(microtime(TRUE) - (self::$delta_t))."]");