5 * Copyright (C) 2006-2012 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);
48 define('CHAT_ENABLED', TRUE);
50 define('SESS_LEN', 13);
51 define('STREAM_TIMEOUT', 60);
52 /* FIXME: move to sac-a-push .phh */
53 /* TIME_RD define the server-side timeout, after half of it a ping request
54 is sent to client, after this time the client is log out */
55 define('EXPIRE_TIME_RD', 180);
56 define('EXPIRE_TIME_SMAMMA', 360);
57 define('EXPIRE_TIME_WAG', 10);
58 define('WAKEUP_TIME', 12);
59 // BAN_TIME da allineare anche in commons.js
60 define('BAN_TIME', 3600);
61 define('GARBAGE_TIMEOUT', 10);
62 define('NICKSERV', "<i>BriskServ</i>");
64 define('LOCK_SHARE_MAX', 10000);
66 define('DBG_ONL2', 0x0001);
67 define('DBG_ONLY', 0x0002);
68 define('DBG_MAIN', 0x0004);
69 define('DBG_READ', 0x0008);
70 define('DBG_REA2', 0x0010);
71 define('DBG_SEND', 0x0020);
72 define('DBG_LOCK', 0x0040);
73 define('DBG_WRIT', 0x0080);
74 define('DBG_LOAD', 0x0100);
75 define('DBG_AUTH', 0x0200);
76 define('DBG_CRIT', 0x0400);
77 define('DBG_LMOP', 0x0800);
78 define('DBG_TRAC', 0x1000);
79 define('DBG_SHME', 0x2000);
80 // NOTE: BRISK DEBUG must be a numerical constant, not the result of operations on symbols
81 define('BRISK_DEBUG', 0x0800);
83 define('BRISK_SINGLE_DEBUG',0);
84 define('BRISK_SINGLE_SESS', "");
85 define('DEBUGGING', "no-debugging");
87 require_once("$DOCUMENT_ROOT/Etc/".BRISK_CONF);
89 $mlang_brisk = array( 'btn_backstand'=> array( 'it' => 'torna in piedi',
90 'en' => 'back standing' ),
91 'btn_close' => array( 'it' => 'chiudi',
94 'tit_all' => array( 'it' => 'tutti',
97 'tabtout_a'=> array( 'it' => '<br>Sei stato inattivo per ',
98 'en' => '<br>You are being idle for ' ),
99 'tabtout_b'=> array( 'it' => ' minuti. <br><br>Quindi ritorni tra i <b>Giocatori in piedi</b>.',
100 'en' => ' minutes. <br><br>Then you return with the <b>standing players</b>.'),
101 'tickmust' => array( 'it' => '<br>Per attivare il messaggio di segnalazione del tavolo occorre essere seduti.<br><br>',
102 'en' => '<br>To activate the signalling message of the table it\'s necessary to be sitting<br><br>'),
103 'tickjust' => array( 'it' => '<br>Il messaggio di segnalazione del tavolo è già attivato.<br><br> ',
104 'en' => 'EN<br>Il messaggio di segnalazione del tavolo è già attivato.<br><br> '),
105 'tickwait' => array( 'it' => '<br>Il messaggio di segnalazione del tavolo<br>è disattivato ancora per %d second%s.<br><br>',
106 'en' => 'EN<br>The signalling message of the table<br>will be deactivated for %d second%s.<br><br>'),
107 'alarpass' => array( 'it' => '<br>La password digitata non è corretta.<br><br>',
108 'en' => '<br>The entered password is not correct.<br><br>'),
109 'alarret' => array( 'it' => '"Alarm \\"<b>%s</b>\\" inviato a <b>%s</b>."',
110 'en' => '"Alarm \\"<b>%s</b>\\" sent to <b>%s</b>."'),
111 'authmust' => array( 'it' => '<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>',
112 'en' => '<b>To authenticate someone you have to be authenticated.</b>'), // on your turn
113 'mesgmust' => array( 'it' => '<b>Per inviare un messaggio devi essere autenticato.</b>',
114 'en' => '<b>To send a message you have to be authenticated.</b>'),
115 'nickmust' => array( 'it' => 'Il nickname deve contenere almeno una lettera dell\'alfabeto o una cifra.',
116 'en' => 'The nickname have to contain at least one letter or one number.'),
117 'nickdupl' => array( 'it' => 'Nickname <b>%s</b> già in uso.',
118 'en' => 'The nickname <b>%s</b> is already in use.'),
119 'authchan' => array( 'it' => '<b>Non puoi cambiare nick a un tavolo per soli autenticati o se sei in modalità isolata.</b>',
120 'en' => '<b>You can\'t change your nickname into a table for only authenticated or if you are in isolation mode.</b>'),
121 '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>',
122 '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 ???
123 'statunkn' => array( 'it' => 'Questo stato non esiste.',
124 'en' => 'This state don\'t exists.'),
125 'tabincon' => array( 'it' => '<br>I dati del tavolo n° %d sono inconsistenti, verranno resettati.<br><br>Torni in piedi.<br><br>',
126 'en' => 'EN <br>I dati del tavolo n° %d sono inconsistenti, verranno resettati.<br><br>Torni in piedi.<br><br>'),
127 'listmust' => array( 'it' => '<b>Per andare in isolamento non bisogna essere seduti a tavoli non riservati.</b>',
128 'en' => '<b>To go to isolation you must don\'t stay on not reserved tables</b>'),
130 'tit_onauth'=>array( 'it' => '(solo aut.)',
131 'en' => '(only aut.)'),
132 'tit_onisol'=>array( 'it' => '(isolam.to)',
133 'en' => '(isolation)'),
134 '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.',
135 'en' => 'Connection to the database failed<br>All authentications are suspended temporarly, you login as normal user.<br>We are about the limitation')
140 $G_lng = langtolng($G_lang);
142 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
143 $G_brisk_version = "4.5.0";
145 /* MLANG: ALL THE INFO STRINGS IN brisk.phh */
146 $root_wellarr = array( 'it' => array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: nuova visualizzazione dello stato della connessione dati.',
147 'Se vuoi iscriverti alla <a target="_blank" href="mailto:ml-briscola+subscribe@milug.org">Mailing List</a>, cliccala!' ),
148 'en' => array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NEWS</b>: usage of reader/writer locking instead of generic exclusive locking.',
149 'If you want to subscribe our <a target="_blank" href="ml-briscola+subscribe@milug.org">Mailing List</a>, click it!' ) );
151 $G_room_help = array( 'it' => '
152 <div style=\\"text-align: left; padding: 8px;\\">
153 <b>Descrizione</b><br>
154 Questa è un\'implementazione della briscola in cinque, così come è spiegata su
155 <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>
156 <b>Configurazione del browser.</b><br>
157 Occorre abilitare i cookies.<br>
159 <b>Uso del sito</b><br>
160 Potete sedervi a un tavolo o rimanere in piedi.<br>
161 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
164 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>
165 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
166 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
167 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
168 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
169 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.
171 <dt><b>Comandi della chat</b>
172 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
173 <dd><b>/tav <i><frase di invito></i></b> - invito per gli altri giocatori al tavolo dove si è seduti
174 <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\\"
175 <dd><b>/authreq</b> - se si è autenticati permette di garantire per un utente fidato
176 <dd><b>/mesgtoadm</b> - se si è autenticati permette di lasciare un messaggio all\'amministratore del sito
177 <dd><b>/listen <all or auth></b> - se si è autenticati permette leggere solo i messaggi degli altri autenticati (auth) o di tutti (all)
183 <div style=\\"text-align: left; padding: 8px;\\">
184 <b>EN Descrizione</b><br>
185 EN Questa è un\'implementazione della briscola in cinque, così come è spiegata su
186 <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>
187 <b>EN Configurazione del browser.</b><br>
188 Occorre abilitare i cookies.<br>
190 <b>Uso del sito</b><br>
191 Potete sedervi a un tavolo o rimanere in piedi.<br>
192 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
195 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>
196 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
197 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
198 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
199 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
200 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.
202 <dt><b>Comandi della chat</b>
203 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
204 <dd><b>/tav <i><frase di invito></i></b> - invito per gli altri giocatori al tavolo dove si è seduti
205 <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\\"
206 <dd><b>/authreq</b> - se si è autenticati permette di garantire per un utente fidato
207 <dd><b>/mesgtoadm</b> - se si è autenticati permette di lasciare un messaggio all\'amministratore del sito
208 <dd><b>/listen <all or auth></b> - se si è autenticati permette leggere solo i messaggi degli altri autenticati (auth) o di tutti (all)
214 $G_room_passwdhowto = array( 'it' => '<br><h2>Come registrarsi su Brisk</h2>
215 <div style=\\"text-align: left; padding: 8px;\\">
216 Attualmente ci sono due metodi per ottenere una password sul sito:<br><br>
218 <li><b>Facendosi garantire da un utente di Brisk che già possidede una password</b><br><br>
219 <li><b>Auto-garantendosi utilizzando uno dei seguenti sistemi di identificazione digitale:</b><br><br>
221 <li>Carta Regionale dei Servizi della Lombardia (la tessera sanitaria)
222 <li>Carta Regionale dei Servizi del Friuli Venezia Giulia (la tessera sanitaria)
223 <li>Smart card di InfoCamere
226 <b>Per auto-garantisi occorre possedere:</b><br><br>
228 <li>il codice PIN della propria carta
229 <li>il lettore di smart-card per collegare la carta al PC (acquistabile di solito presso le edicole)
232 <b>Per effettuare la registrazione collegarsi al sito:</b><br><br>
234 <dd><a class=\\"flat\\" target=\\"_blank\\" href=\\"https://brisk.mine.nu\\">https://brisk.mine.nu</a>
237 Se sei in possesso di una carta che permette l\'identificazione via internet che non è nell\'elenco qui sopra
238 <a class=\\"flat\\" href=\\"mailto:authadmbrisk@alternativeoutput.it\\">fai una segnalazione</a>.
243 'en' => '<br><h2>EN Come registrarsi su Brisk</h2>
244 <div style=\\"text-align: left; padding: 8px;\\">
245 EN Attualmente ci sono due metodi per ottenere una password sul sito:<br><br>
247 <li><b>Facendosi garantire da un utente di Brisk che già possidede una password</b><br><br>
248 <li><b>Auto-garantendosi utilizzando uno dei seguenti sistemi di identificazione digitale:</b><br><br>
250 <li>Carta Regionale dei Servizi della Lombardia (la tessera sanitaria)
251 <li>Carta Regionale dei Servizi del Friuli Venezia Giulia (la tessera sanitaria)
254 <b>Per auto-garantisi occorre possedere:</b><br><br>
256 <li>il codice PIN della propria carta
257 <li>il lettore di smart-card per collegare la carta al PC (acquistabile di solito presso le edicole)
260 <b>Per effettuare la registrazione collegarsi al sito:</b><br><br>
262 <dd><a class=\\"flat\\" target=\\"_blank\\" href=\\"https://brisk.mine.nu\\">https://brisk.mine.nu</a>
265 Se sei in possesso di una carta che permette l\'identificazione via internet che non è nell\'elenco qui sopra
266 <a class=\\"flat\\" href=\\"mailto:authadmbrisk@alternativeoutput.it\\">fai una segnalazione</a>.
272 <dd>Seguendo la procedura di auto-garanzia all\'url: <a href="https://brisk.mine.nu">https://brisk.mine.nu</a>
276 $G_room_about = array( 'it' => '<br>
277 <div id=\\"header\\" class=\\"header\\">
278 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
279 briscola chiamata in salsa ajax
281 <br><b>version '.$G_brisk_version.'</b><br><br>
282 Copyright 2006-2012 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>',
284 <div id=\\"header\\" class=\\"header\\">
285 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
286 declaration briscola in ajax sauce <b>(Beta)</b>
288 <br><b>version '.$G_brisk_version.'</b><br><br>
289 Copyright 2006-2012 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>');
293 for ($i = 0; $i < ob_get_level(); $i++)
295 ob_implicit_flush(1);
299 function force_no_cache(&$header_out)
301 $header_out['Pragma'] = 'no-cache, must-revalidate';
302 $header_out['Cache-Control'] = 'no-cache';
303 $header_out['Expires'] = '-1';
306 function file_lock($fname, $is_exclusive)
308 if (($res = @fopen($fname, "r+")) == FALSE) {
312 if (flock($res, ($is_exclusive ? LOCK_EX : LOCK_SH)) == FALSE) {
320 function file_unlock($res)
323 flock($res, LOCK_UN);
328 function webservers_exceeded()
330 return(file_exists(PROXY_PATH."/webservers_exceded.flag"));
333 function webservers_check()
335 GLOBAL $G_webserver_max;
337 /* FIXME: check all procs expirations */
342 $dh = opendir('/proc');
343 while (($file = readdir($dh)) !== false) {
344 if (preg_match('/[0-9]+/', $file)) {
345 $cmdline = explode("\0", file_get_contents('/proc/'.$file.'/cmdline'));
346 // echo "xxx".$cmdline[0].$n;
347 if (strstr('/usr/sbin/apache2', $cmdline[0]) != FALSE) {
348 // echo "yyy".$cmdline[0].$n;
355 if ($ct >= $G_webserver_max) {
356 touch(PROXY_PATH."/webservers_exceded.flag");
359 unlink(PROXY_PATH."/webservers_exceded.flag");
364 $escpush_from = array("\\", "\"");
365 $escpush_to = array("\\\\", "\\\"");
368 GLOBAL $escpush_from, $escpush_to;
370 return str_replace($escpush_from, $escpush_to, $s);
373 $escinp_from = array( "\"" );
374 $escinp_to = array( """ );
376 function escinput($s)
378 GLOBAL $escinp_from, $escinp_to;
380 return str_replace($escinp_from, $escinp_to, $s);
385 return htmlentities($s);
388 function esclfhtml($s)
390 return str_replace("\n", "<br>\n", htmlentities($s));
394 function langtolng($lang)
398 return ($G_lang == 'en' ? '-en' : '');
401 function csplitter($in, $sep)
407 for ($i = 0 ; $i < strlen($in) ; $i++) {
408 $ini = substr($in, $i, 1);
412 else if ($ini == $sep) {
431 $from = array ( '\\', '@', '|' );
432 $to = array ( '\\\\', '@', '¦' );
434 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
439 $from = array ( '\\', '|', "\t", "\n");
440 $to = array ( '\\\\', '\\|', "\\t", "\\n");
442 return (str_replace($from, $to, $s));
445 function xcapemesg($s)
447 $from = array ( "\n");
448 $to = array ( "\\n");
450 return (str_replace($from, $to, $s));
460 function getbyid($idx)
462 return ($this->el[$idx]);
465 function setbyid($idx, $v)
467 $this->el[$idx] = $v;
476 var $auth_only; // se tavolo riservato o libero
483 var $table_start; // information field
491 function create($idx)
493 if (($thiz = new Table()) == FALSE)
497 $thiz->player = array();
499 $thiz->auth_only = FALSE;
505 $thiz->table_token = "";
506 $thiz->table_start = 0;
508 $thiz->wakeup_time = 0;
515 $this->idx = $from->idx;
516 $this->player = array();
517 for ($i = 0 ; $i < $from->player_n ; $i++)
518 $this->player[$i] = $from->player[$i];
519 $this->player_n = $from->player_n;
521 log_main("PLAYER_N - parent::copy.".$this->player_n);
523 $this->auth_only = $from->auth_only;
525 $this->wag_own = $from->wag_own;
526 $this->wag_com = $from->wag_com;
527 $this->wag_tout = $from->wag_tout;
529 $this->table_token = $from->table_token;
530 $this->table_start = $from->table_start;
532 $this->wakeup_time = $from->wakeup_time;
535 function myclone($from)
537 if (($thiz = new Table()) == FALSE)
545 function spawn($from)
547 if (($thiz = new Table()) == FALSE)
550 $thiz->idx = $from->idx;
551 $thiz->player = array();
552 for ($i = 0 ; $i < $from->player_n ; $i++)
553 $thiz->player[$i] = $i;
554 $thiz->player_n = $from->player_n;
556 $thiz->auth_only = $from->auth_only;
558 $thiz->wag_own = $from->wag_own;
559 $thiz->wag_com = $from->wag_com;
560 $thiz->wag_tout = $from->wag_tout;
562 $thiz->table_token = $from->table_token;
563 $thiz->table_start = $from->table_start;
565 $thiz->wakeup_time = $from->wakeup_time;
570 function wag_set($user_idx, $mesg)
574 $this->wag_own = $user_idx;
575 $this->wag_com = $mesg;
579 function wag_reset($timeout)
581 log_main("WAG_RESET");
583 unset($this->wag_own);
586 $this->wag_tout = $timeout;
589 function player_get($idx)
591 return ($this->player[$idx]);
594 function player_set($idx, $player)
596 $this->player[$idx] = $player;
599 function user_add($idx)
601 $this->player[$this->player_n] = $idx;
604 return ($this->player_n - 1);
607 function user_rem($room, $user)
609 $tabpos = $user->table_pos;
611 /* verifico la consistenza dei dati */
612 if ($room->user[$this->player[$tabpos]] == $user) {
614 /* aggiorna l'array dei giocatori al tavolo. */
615 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
616 $this->player[$i] = $this->player[$i+1];
617 $user_cur = $room->user[$this->player[$i]];
618 $user_cur->table_pos = $i;
623 log_main("INCONSISTENCY ON TABLE.");
629 // $ret .= table_act_content(($user->subst == 'standup'), $this->table[$i]->player_n, $i, $user->table,
630 // ($this->table[$i]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
632 // function act_content($isstanding, $sitted, $table, $cur_table, $allowed)
633 function act_content($user)
636 $isstanding = ($user->subst == 'standup');
637 $sitted = $this->player_n;
639 $cur_table = $user->table;
643 if ($sitted < PLAYERS_N) {
644 if ($this->auth_only) {
645 if ($user->flags & USER_FLAG_AUTH)
659 if ($table == $cur_table)
666 $ret = sprintf('j_tab_act_cont(%d, \'%s\');', $table, $act);
679 var $crystal_filename;
683 var $comm; // commands for many people
684 var $step; // current step of the comm array
685 var $garbage_timeout;
688 function Room ($crystal_filename) {
689 $this->crystal_filename = $crystal_filename;
690 $this->user = array();
691 $this->table = array();
692 $this->match = array();
694 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
695 $this->user[$i] = User::create($this, $i, "", "");
698 for ($i = 0 ; $i < TABLES_N ; $i++) {
699 $this->table[$i] = Table::create($i);
702 $row = ( (((int)($i / 4)) % 2) == 0 );
703 $col = ($i % 2 == 0);
704 $this->table[$i]->auth_only = (($row && $col) || (!$row && !$col));
707 $this->table[$i]->auth_only = FALSE;
710 if ($i < TABLES_AUTH_N)
711 $this->table[$i]->auth_only = TRUE;
713 $this->table[$i]->auth_only = FALSE;
715 $this->garbage_timeout = 0;
716 $this->shm_sz = SHM_DIMS_MIN;
719 function garbage_manager($force)
721 GLOBAL $G_lang, $mlang_brisk, $G_base;
725 log_rd2("garbage_manager START");
727 /* Garbage collector degli utenti in timeout */
730 if (!$force && !($this->garbage_timeout < $curtime)) {
736 // Before all align times with table timeout
737 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
738 $table_cur = $this->table[$table_idx];
739 // if the table is complete and exists its shared mem we get the info about users lacc
741 if ($table_cur->player_n == PLAYERS_N) {
742 log_main("PLAYERS == N TABLE ".$table_idx);
745 $no_recovery = FALSE;
746 if (isset($this->match[$table_idx])) {
747 $bri = $this->match[$table_idx];
749 if ($table_cur->table_token != $bri->table_token) {
750 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
751 log_main("ERROR: not matching table_start. Room: ".$table_cur->table_start." Table: ".$bri->table_start);
761 log_main("garbage_manager: bri loaded successfully.");
762 $bri->garbage_manager(TRUE);
764 $bri_table = $bri->table[0];
766 // is the end of the table
768 if ($bri->the_end == TRUE) {
770 * DESTROY OF FINISHED TABLE && MOVE PLAYER TO ROOM AGAIN
772 log_main("garbage_manager: INSIDE THE END.");
774 $plist = "$table_cur->table_token|$table_cur->idx|$table_cur->player_n";
775 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
776 $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
779 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
780 // stat must be "table" by definition
781 $user_cur = $this->user[$table_cur->player[$i]];
782 $bri_user = $bri->user[$i];
784 $user_cur->subst = $bri_user->subst;
785 $user_cur->step = $bri_user->step;
786 $user_cur->lacc = $bri_user->lacc;
787 $user_cur->laccwr = $bri_user->lacc;
788 $user_cur->bantime = $bri_user->bantime;
791 log_legal($curtime, 'xxx', $user_cur, "STAT:DESTROY_GAME", $plist);
793 $this->room_join_wakeup($user_cur, FALSE, 0);
794 $table_cur->table_token = "";
795 $table_cur->wakeup_time = $curtime + WAKEUP_TIME;
797 $this->match_del($table_idx);
800 log_main("gm:: save_data");
802 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
803 $this->user[$table_cur->player[$i]]->lacc = $bri->user[$i]->lacc;
806 } // if ($bri == FALSE
807 else if ($no_recovery == FALSE) {
808 log_crit("ERROR: table ".$table_idx." unrecoverable join");
810 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
811 $user_cur = $this->user[$table_cur->player[$i]];
812 $user_cur->subst = "shutdowner";
813 $user_cur->step_inc();
815 $ret = sprintf('stat = "%s"; subst = "%s";', $user_cur->stat, $user_cur->subst);
816 $ret .= "gst.st = ".($user_cur->step+1)."; ";
817 // MLANG <br>I dati del tavolo n° ".$user_cur->table." sono inconsistenti, verranno resettati.<br><br>Torni in piedi.<br><br>
818 $prestr = sprintf($mlang_brisk['tabincon'][$G_lang], $user_cur->table);
819 $ret .= show_notify($prestr, 2000, $mlang_brisk['btn_close'][$G_lang], 400, 110);
820 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
821 $user_cur->step_inc();
824 $plist = "$table_cur->table_token|$user_cur->table|$table_cur->player_n";
825 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
826 $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
828 log_legal($curtime, 'xxx', $user_cur, "STAT:DESTROY_GAME(RECOVERY)", $plist);
830 $this->room_join_wakeup($user_cur, TRUE, -2);
831 $table_cur->table_token = "";
834 } // if ($table_cur->player_n == PLAYERS_N) {
835 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
837 log_rd2("out new loop.");
839 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
840 $user_cur = $this->user[$i];
842 log_rd2("User: ".$user_cur->name." stat: ".$user_cur->stat." subst: ".$user_cur->subst);
844 if ($user_cur->sess == "")
847 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) {
848 // Auto logout dell'utente
849 log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime ".$curtime);
851 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
852 log_auth($user_cur->sess, "Autologout session.");
856 log_rd2("AUTO LOGOUT.");
857 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
858 $this->room_wakeup($user_cur);
859 else if ($user_cur->subst == 'standup')
860 $this->room_outstandup($user_cur);
862 log_rd2("LOGOUT FROM WHAT ???");
866 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
867 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
868 $this->room_wakeup($user_cur);
869 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
870 /* 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" */
871 $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);
872 $user_cur->step_inc();
876 log_rd2("GARBAGE UPDATED!");
878 $this->garbage_timeout = $curtime + GARBAGE_TIMEOUT;
884 function show_room($user_step, $user)
886 GLOBAL $G_lang, $mlang_brisk;
887 log_main("show_room: username: ".$user->name);
889 $ret = sprintf('gst.st = %d; ', $user_step);
891 if ($user->flags & USER_FLAG_ISOLAUTH) {
892 $ret .= 'list_set(\'isolation\', false, \''.$mlang_brisk['tit_onisol'][$G_lang].'\' ); ';
894 else if ($user->flags & USER_FLAG_LISTAUTH) {
895 $ret .= 'list_set(\'auth\', false, \''.$mlang_brisk['tit_onauth'][$G_lang].'\' ); ';
898 $ret .= 'list_set(\'all\', false, \'\' ); ';
901 if ($user->subst == 'standup')
902 $ret .= "tra.show(); ";
904 $ret .= "tra.hide(); ";
906 $ret .= sprintf('stat = "%s";', $user->stat);
908 $ret .= root_wellcome($user);
909 if ($user->flags & USER_FLAG_DBFAILED) {
910 $ret .= "gst.st = ".($user->step+1)."; ";
911 $ret .= show_notify($mlang_brisk['db_failed'][$G_lang], 0, $mlang_brisk['btn_close'][$G_lang], 400, 140);
914 $ret .= sprintf('subst = "%s";', $user->subst);
915 $ret .= $user->myname_innerHTML();
917 for ($i = 0 ; $i < TABLES_N ; $i++) {
919 $ret .= $this->table_content($user, $i);
920 // $ret .= table_act_content(($user->subst == 'standup'), $this->table[$i]->player_n, $i, $user->table,
921 // ($this->table[$i]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
922 $ret .= $this->table[$i]->act_content($user);
923 if ($this->table[$i]->wag_own != -1)
924 $ret .= sprintf('tra.add(%d, "%s: %s"); ', $i, $this->user[$this->table[$i]->wag_own]->name, $this->table[$i]->wag_com);
926 $ret .= sprintf('tra.rem(%d); ', $i);
928 $ret .= $this->standup_content($user);
929 $ret .= "setTimeout(preload_images, 0, g_preload_img_arr, g_imgct); ";
935 function room_wakeup($user)
937 $table_idx = $user->table;
938 $table = $this->table[$table_idx];
940 log_main("WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
944 $from_table = ($user->stat == "table");
946 log_main("WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
948 for ($i = 0 ; $i < $table->player_n ; $i++) {
949 $user_cur = $this->user[$table->player[$i]];
950 log_main("PREIMPOST: INLOOP name: ".$user_cur->name);
952 if ($user->idx_get() != $table->player[$i]) {
953 $user_cur->stat_set("room");
954 $user_cur->subst = "sitdown";
955 $user_cur->laccwr = $curtime;
957 else if ($user->sess != "") {
958 $user_cur->stat_set("room");
959 $user_cur->subst = "standup";
960 $user_cur->laccwr = $curtime;
961 $user_cur->table = -1;
966 $user->stat_set("room");
967 $user->subst = "standup";
968 $user->laccwr = $curtime;
971 $remove_wagon = FALSE;
972 if($table->wag_own == $user->idx_get()) {
973 $table->wag_reset($curtime);
974 $remove_wagon = TRUE;
978 /* aggiorna l'array dei giocatori al tavolo. */
979 $table->user_rem($this, $user);
981 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
982 $user_cur = $this->user[$i];
983 if ($user_cur->sess == '' || $user_cur->stat != 'room')
986 // log_main("VALORI: name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
988 $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
989 if ($from_table && ($user_cur->table == $table_idx || $user->idx_get() == $i)) {
990 $ret .= 'gst.st_loc++; xstm.stop(); window.onunload = null; window.onbeforeunload = null; document.location.assign("index.php");|';
991 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
992 log_main("DOCUMENT.index.php: from table");
994 else if ($user_cur->stat == "room") {
995 log_main("DOCUMENT.index.php: from table");
997 $ret .= $this->table_content($user_cur, $table_idx);
998 $ret .= $this->standup_content($user_cur);
1000 // $ret .= table_act_content(FALSE, 0, $table_idx, $user->table, FALSE);
1001 $ret .= $table->act_content($user);
1003 if ($user->idx_get() == $i) {
1004 // set the new status
1005 $ret .= 'subst = "standup"; tra.show(); ';
1006 // clean the action buttons in other tables
1007 for ($e = 0 ; $e < TABLES_N ; $e++) {
1008 if ($this->table[$e]->player_n < PLAYERS_N) {
1009 // $ret .= table_act_content(TRUE, 0, $e, $user->table,
1010 // ($this->table[$e]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
1011 $ret .= $this->table[$e]->act_content($user);
1016 // $ret .= table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table,
1017 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1018 $ret .= $table->act_content($user_cur);
1021 log_wr("ROOM_WAKEUP: ".$ret);
1022 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1023 $user_cur->step_inc();
1027 function room_join_wakeup($user, $update_lacc = FALSE, $trans_delta)
1029 $table_idx = $user->table;
1030 $table = $this->table[$table_idx];
1032 log_main("JOIN_WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
1035 $user_wup = array();
1037 $user_tab = array();
1039 log_main("JOIN WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1041 for ($i = 0 ; $i < $table->player_n ; $i++) {
1042 $user_cur = $this->user[$table->player[$i]];
1043 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1044 if ($user_cur->sess != "") {
1045 if ($update_lacc == TRUE) {
1046 $user_cur->laccwr = $curtime;
1048 log_main("cur: ".$user_cur->name." subst: ".$user_cur->subst);
1049 if ($user_cur->subst == "shutdowned") {
1050 $user_cur->stat_set("room");
1051 $user_cur->subst = "sitdown";
1053 else if ($user_cur->subst == "shutdowner") {
1054 $user_cur->stat_set("room");
1055 $user_cur->subst = "standup";
1056 $user_cur->table = -1;
1057 $user_wup[$user_wup_n++] = $user_cur;
1059 $remove_wagon = FALSE;
1060 if($table->wag_own == $table->player[$i]) {
1061 $remove_wagon = TRUE;
1062 $table->wag_reset($curtime);
1065 $user_tab[$user_tab_n++] = $table->player[$i];
1069 for ($wup_idx = 0 ; $wup_idx < $user_wup_n ; $wup_idx++)
1070 $table->user_rem($this, $user_wup[$wup_idx]);
1072 /* aggiorna l'array dei giocatori al tavolo. */
1074 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1075 log_main("START LOOP");
1076 $user_cur = $this->user[$i];
1077 if ($user_cur->sess == '' || $user_cur->stat != 'room') {
1078 log_main("name: ".$user_cur->name."skip subst: ".$user_cur->subst);
1083 log_main("VALORI name: ".$user_cur->name." tab: ".$user_cur->table." taix: ".$table_idx);
1085 $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
1086 if ($user_cur->stat == "room") {
1087 log_main("DOCUMENT.index.php from table");
1089 $ret .= $this->table_content($user_cur, $table_idx);
1090 $ret .= $this->standup_content($user_cur);
1092 // $ret .= table_act_content(FALSE, 0, $table_idx, $user_cur->table,
1093 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1094 $ret .= $table->act_content($user_cur);
1097 for ($tab_idx = 0 ; $tab_idx < $user_tab_n ; $tab_idx++)
1098 if ($user_tab[$tab_idx] == $i)
1101 // for users that wakeup the room will be reconstructed by index_rd.php
1102 if ($tab_idx < $user_tab_n) {
1103 log_main("PRE show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1105 // ARRAY_POP DISABLED
1106 // if ($trans_delta == 0)
1107 // while (array_pop($user_cur->comm) != NULL);
1109 $user_cur->trans_step = $user_cur->step + 1 + $trans_delta;
1110 $user_cur->comm[$user_cur->step % COMM_N] = "";
1111 $user_cur->step_inc();
1112 $user_cur->comm[$user_cur->step % COMM_N] = $this->show_room(($user_cur->step + 1), $user_cur);
1113 $user_cur->step_inc();
1114 log_main("POST show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1118 log_main("JOIN_WAKEUP wup_idx ".$wup_idx." wup_n ".$user_wup_n);
1120 log_main("JOIN_WAKEUP more");
1121 // $ret .= table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table,
1122 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1123 $ret .= $table->act_content($user_cur);
1125 log_main("JOIN_WAKEUP end more");
1127 log_wr("ROOM_JOIN_WAKEUP: ".$ret);
1128 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1129 $user_cur->step_inc();
1133 function room_outstandup($user)
1135 $this->room_sitdown($user, -1);
1138 function table_update($user)
1140 log_main("table_update: pre - USER: ".$user->name);
1142 $table_idx = $user->table;
1144 if ($table_idx > -1)
1145 $table = $this->table[$table_idx];
1147 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1149 $user_cur = $this->user[$i];
1150 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1153 $ret = "gst.st = ".($user_cur->step+1)."; ";
1154 if ($table_idx > -1)
1155 $ret .= $this->table_content($user_cur, $table_idx);
1157 if ($user->idx_get() == $i) {
1158 $ret .= $user->myname_innerHTML();
1160 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1161 $user_cur->step_inc();
1164 log_main("table_update: post");
1167 function room_sitdown($user, $table_idx)
1169 log_main("room_sitdown ".($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
1173 if ($table_idx > -1 && $table_idx < TABLES_N) {
1174 $table = $this->table[$table_idx];
1177 if ($table->wag_own != -1 && $table->player_n == PLAYERS_N) {
1178 for ($i = 0 ; $i < TABLES_N ; $i++) {
1179 if ($table->wag_own == $table->player[$i]) {
1180 $train_app = sprintf("tra.rem(%d); ", $table_idx);
1181 $table->wag_reset(time());
1188 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1190 $user_cur = $this->user[$i];
1191 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1194 $ret = "gst.st = ".($user_cur->step+1)."; ".$train_app;
1195 if ($table_idx > -1)
1196 $ret .= $this->table_content($user_cur, $table_idx);
1197 $ret .= $this->standup_content($user_cur);
1199 if ($user->idx_get() == $i) {
1200 $ret .= 'subst = "sitdown"; tra.hide(); ';
1201 // clean the action buttons in other tables
1202 for ($e = 0 ; $e < TABLES_N ; $e++) {
1203 // $ret .= table_act_content(FALSE, 0, $e, $user_cur->table, FALSE);
1204 $ret .= $this->table[$e]->act_content($user_cur);
1207 else if ($table_idx > -1) {
1208 if ($table->player_n == PLAYERS_N) {
1209 // $ret .= table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table,
1210 /// ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1211 $ret .= $table->act_content($user_cur);
1214 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1215 $user_cur->step_inc();
1219 function chatt_send($user, $mesg)
1221 GLOBAL $G_base, $G_alarm_passwd, $mlang_brisk, $G_lang;
1225 $msg = mb_substr($mesg, 6, 128, "UTF-8");
1227 $dt = date("H:i ", $curtime);
1238 $is_normchat = FALSE;
1239 /* for old isolation management $is_ticker = FALSE; */
1240 $update_room = FALSE;
1242 if (strcmp($msg, "/tav") == 0 ||
1243 strncmp($msg, "/tav ", 5) == 0) {
1245 if ($user->stat != 'room' || $user->subst != 'sitdown') {
1246 /* 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>" */
1247 $msg = $mlang_brisk['tickmust'][$G_lang];
1248 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1253 $table = $this->table[$user->table];
1255 if ($table->wag_own != -1) {
1256 // MLANG <br>Il messaggio di segnalazione del tavolo è già attivato.<br><br>
1257 $msg = $mlang_brisk['tickjust'][$G_lang];
1258 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1263 $dtime = $curtime - $table->wag_tout;
1264 if ($dtime < EXPIRE_TIME_WAG) {
1265 // MLANG - <br>Il messaggio di segnalazione del tavolo<br>è disattivato ancora per %d second%s.<br><br>
1266 $msg = sprintf($mlang_brisk['tickwait'][$G_lang],
1267 EXPIRE_TIME_WAG - $dtime, (EXPIRE_TIME_WAG - $dtime == 1 ? ($G_lang == 'en' ? "" : "o") : ($G_lang == 'en' ? "s" : "i")));
1268 $to_user = show_notify($msg, 0, $mlang_brisk['btn_close'][$G_lang] , 400, 100);
1273 $msg = substr($msg, 5);
1275 $table->wag_set($user->idx_get(), $msg);
1276 $to_user = sprintf('tra.add(%d, "%s");', $user->table, xcape(sprintf("%s: %s", $user->name, $msg)));
1277 $to_room = $to_user;
1278 /* for old isolation management $is_ticker = TRUE; */
1280 } // /tav chat command
1282 else if (strncmp($msg, "/alarm ", 7) == 0) {
1283 if (strncmp($msg, "/alarm to ", 10) == 0) {
1284 $sp_pos = strpos($msg, " ", 10);
1285 $target = substr($msg, 10, $sp_pos - 10);
1286 $alarm_check = "/alarm to ".$target." ".$G_alarm_passwd." ";
1290 $alarm_check = "/alarm ".$G_alarm_passwd." ";
1293 if (strncmp($msg, $alarm_check, strlen($alarm_check)) != 0) {
1294 /* MLANG: "<br>La password digitata non è corretta.<br><br>" */
1295 $msg = $mlang_brisk['alarpass'][$G_lang];
1296 $to_user = show_notify($msg, 0, $mlang_brisk['btn_close'][$G_lang], 400, 100);
1301 /* MLANG: "Alarm <b>%s</b> inviato a <b>%s</b>." */
1302 $prestr = sprintf($mlang_brisk['alarret'][$G_lang], xcape(substr($msg, strlen($alarm_check))),
1303 ($target == "" ? $mlang_brisk['tit_all'][$G_lang] : xcape($target)) );
1304 $to_user = sprintf('chatt_sub("%s", [2, "%s"],%s);',
1305 $dt, NICKSERV, $prestr);
1307 $msg = sprintf("<br><b>%s<br><br>%s</b><br><br>",
1308 $dt.NICKSERV, xcape(substr($msg, strlen($alarm_check))));
1309 /* MLANG: "chiudi" */
1310 $to_all = show_notify($msg, 0, $mlang_brisk['btn_close'][$G_lang], 400, 120);
1312 } // /alarm chat command
1313 else if (strncmp($msg, "/listen ", 8) == 0) {
1314 $arg = substr($msg, 8);
1316 if (strcasecmp($arg, "isolation") == 0) {
1318 if ($user->stat == 'room' && $user->subst == 'sitdown' &&
1319 $user->table >= TABLES_AUTH_N) {
1320 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['listmust'][$G_lang]);
1324 $user->flags &= ~USER_FLAG_MAP_AUTH;
1325 $user->flags |= USER_FLAG_ISOLAUTH;
1326 $to_user = 'list_set(\'isolation\', true, \''.$mlang_brisk['tit_onisol'][$G_lang].'\'); ';
1329 else if (strcasecmp($arg, "auth") == 0) {
1330 $flags_old = $user->flags;
1331 $user->flags &= ~USER_FLAG_MAP_AUTH;
1332 $user->flags |= USER_FLAG_LISTAUTH;
1333 $to_user = 'list_set(\'auth\', true, \''.$mlang_brisk['tit_onauth'][$G_lang].'\'); ';
1336 $flags_old = $user->flags;
1337 $user->flags &= ~USER_FLAG_MAP_AUTH;
1338 $to_user = 'list_set(\'all\', true, \'\'); ';
1341 // if from isolation redraw standup area
1342 if (($flags_old ^ $user->flags) & USER_FLAG_ISOLAUTH) {
1343 $to_user .= 'standup_data_old = null; '.$this->standup_content($user);
1347 else if (strcmp($msg, "/authreq") == 0) {
1348 if ($user->flags & USER_FLAG_AUTH) {
1349 $to_user = sprintf('authbox(300,200);');
1352 /* 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." */
1353 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['authmust'][$G_lang]);
1356 else if (strncmp($msg, "/mesgtoadm", 8) == 0) {
1357 if ($user->flags & USER_FLAG_AUTH) {
1358 $to_user = sprintf('mesgtoadmbox(500,300);');
1361 /* MLANG: "<b>Per inviare un messaggio devi essere autenticato.</b>" */
1362 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['mesgmust'][$G_lang]);
1365 else if (strncmp($msg, "/nick ", 6) == 0) {
1366 log_main("chatt_send BEGIN");
1369 if (($name_new = validate_name(substr($msg, 6))) == FALSE) {
1370 $to_user = sprintf('chatt_sub("%s", [2,"%s"],"%s");', $dt, NICKSERV, $mlang_brisk['nickmust'][$G_lang]);
1374 $msg = "COMMAND ".$msg;
1375 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1376 $user_cur = $this->user[$i];
1378 if ($user_cur->sess == '')
1380 if (strcasecmp($user_cur->name,$name_new) == 0)
1383 if ($i < MAX_PLAYERS) {
1384 $prestr = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
1385 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $prestr);
1389 /* 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>" */
1390 if ($user->flags & USER_FLAG_AUTH) {
1391 if (strcasecmp($user->name,$name_new) != 0) {
1392 if (( ($user->flags & USER_FLAG_MAP_AUTH) != USER_FLAG_ISOLAUTH) &&
1393 ($user->subst == 'standup' ||
1394 ($user->subst != 'standup' && $this->table[$user->table]->auth_only == FALSE)
1397 $user->flags &= ~(USER_FLAG_AUTH | USER_FLAG_TY_ALL); // Remove auth if name changed
1398 for ($i = 0 ; $i < TABLES_N ; $i++) {
1399 $to_user .= $this->table[$i]->act_content($user);
1403 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['authchan'][$G_lang]);
1408 $user->name = $name_new; // OK - nick changed
1409 /* se nome gia' in uso, segnala cosa potrebbe capitare */
1410 if (($user->flags & USER_FLAG_AUTH) == 0) {
1411 if (($bdb = BriskDB::create()) != FALSE) {
1413 /* 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>" */
1414 if ($bdb->login_exists($name_new)) {
1415 $prestr = sprintf($mlang_brisk['nickjust'][$G_lang], xcape($name_new));
1416 $to_user .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $prestr);
1421 log_main("chatt_send start set");
1423 $update_room = TRUE;
1425 } // nick chat command
1427 else if (strncmp($msg, "/st ", 4) == 0) {
1428 log_main("chatt_send BEGIN");
1431 $st_str = substr($msg, 4);
1433 if (strcasecmp($st_str, "normale") == 0) {
1434 $st = USER_FLAG_S_NORM;
1436 else if (strcasecmp($st_str, "pausa") == 0) {
1437 $st = USER_FLAG_S_PAU;
1439 else if (strcasecmp($st_str, "fuori") == 0) {
1440 $st = USER_FLAG_S_OUT;
1442 else if (strcasecmp($st_str, "cane") == 0) {
1443 $st = USER_FLAG_S_DOG;
1445 else if (strcasecmp($st_str, "cibo") == 0) {
1446 $st = USER_FLAG_S_EAT;
1448 else if (strcasecmp($st_str, "lavoro") == 0) {
1449 $st = USER_FLAG_S_WRK;
1451 else if (strcasecmp($st_str, "sigaretta") == 0) {
1452 $st = USER_FLAG_S_SMK;
1454 else if (strcasecmp($st_str, "presente") == 0) {
1455 $st = USER_FLAG_S_EYE;
1457 else if (strcasecmp($st_str, "coniglio") == 0) {
1458 $st = USER_FLAG_S_RABB;
1460 else if (strcasecmp($st_str, "calcio") == 0) {
1461 $st = USER_FLAG_S_SOCC;
1463 else if (strcasecmp($st_str, "pupo") == 0) {
1464 $st = USER_FLAG_S_BABY;
1466 else if (strcasecmp($st_str, "pulizie") == 0) {
1467 $st = USER_FLAG_S_MOP;
1469 else if (strcasecmp($st_str, "babbo") == 0) {
1470 $st = USER_FLAG_S_BABBO;
1472 else if (strcasecmp($st_str, "renna") == 0) {
1473 $st = USER_FLAG_S_RENNA;
1475 else if (strcasecmp($st_str, "pupazzo") == 0) {
1476 $st = USER_FLAG_S_PUPAZ;
1478 else if (strcasecmp($st_str, "vischio") == 0) {
1479 $st = USER_FLAG_S_VISCH;
1482 /* MLANG: "Questo stato non esiste." */
1483 $to_user = sprintf('chatt_sub("%s", [2,"%s"],"%s");', $dt, NICKSERV, $mlang_brisk['statunkn'][$G_lang]);
1487 log_main("chatt_send start set");
1488 if (($user->flags & USER_FLAG_S_ALL) != $st) {
1489 $update_room = TRUE;
1490 $user->flags = ($user->flags & ~USER_FLAG_S_ALL) | $st;
1493 } // nick chat command
1495 else { // normal chat line
1496 $is_normchat = TRUE;
1497 if (CHAT_ENABLED && $curtime < ($user->chat_ban + $user->chat_dlt)) {
1499 $user->chat_dlt = $user->chat_dlt * 2;
1500 if ($user->chat_dlt > 120)
1501 $user->chat_dlt = 120;
1503 else if ($user->chat_lst == $msg)
1505 else if (CHAT_ENABLED && $curtime - $user->chattime[($user->chat_cur + 1) % CHAT_N] < CHAT_ILL_TIME) {
1506 $user->chat_ban = $curtime;
1507 $user->chat_dlt = 5;
1511 $user->chat_ban = 0;
1512 $user->chat_dlt = 0;
1516 $to_user = sprintf('chatt_sub("%s", [%d, "%s"],"%s");', $dt, $user->flags, xcape($user->name), xcape("== chat ban =="));
1519 $to_user = sprintf('chatt_sub("%s", [%d, "%s"],"%s");', $dt, $user->flags, xcape($user->name), xcape($msg));
1520 // temporary silentiation for troll (will became array check)
1521 // if (strcasecmp($user->name,'JackRokka') != 0 && $user->sess != '47ea653f602e8')
1522 $to_room = $to_user;
1525 log_legal($curtime, 'xxx', $user,
1526 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$msg);
1528 $user->chat_lst = "$msg";
1529 $user->chattime[$user->chat_cur % CHAT_N] = $curtime;
1539 // Output to clients
1542 if ($to_user != FALSE) {
1543 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1544 $user->comm[$user->step % COMM_N] .= $to_user;
1548 if ($to_room != FALSE) {
1549 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1550 $user_cur = $this->user[$i];
1551 if ($target != "" && $user_cur->name != $target)
1553 if ($user_cur->sess == '' || $user_cur->stat == 'table' || $user->idx_get() == $i)
1556 if ($is_normchat == TRUE) {
1557 // use MAP_AUTH to check if auth or isolation
1558 if ($user_cur->flags & USER_FLAG_MAP_AUTH) {
1559 if (($user->flags & USER_FLAG_AUTH) == 0) {
1565 else if ($is_ticker) {
1566 if (($user_cur->flags & USER_FLAG_MAP_AUTH) == USER_FLAG_ISOLAUTH) {
1567 if ($user->table >= TABLES_AUTH_N)
1572 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1573 $user_cur->comm[$user_cur->step % COMM_N] .= $to_room;
1574 $user_cur->step_inc();
1579 // FIXME BRISK4: include for each kind of table
1580 require_once("${G_base}briskin5/Obj/briskin5.phh");
1581 // Before all align times with table timeout
1582 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1583 if (isset($this->match[$table_idx])) {
1584 $bri = $this->match[$table_idx];
1586 $bri_table = $bri->table[0];
1587 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
1588 // stat must be "table" by definition
1589 $bri_user = $bri->user[$i];
1591 if ($target != "" && $bri_user->name != $target)
1593 log_main("writa: ".$user_mesg);
1594 $bri_user->comm[$bri_user->step % COMM_N] = "gst.st = ".($bri_user->step+1)."; ";
1595 $bri_user->comm[$bri_user->step % COMM_N] .= $to_tabl;
1596 $bri_user->step_inc();
1598 } // if (isset($this->match
1599 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1600 } // if ($to_tabl == true ...
1603 if ($user->stat == 'room' && $user->subst == 'standup') {
1604 $this->standup_update($user);
1606 else if ($user->stat == 'room' && $user->subst == 'sitdown') {
1607 log_main("chatt_send pre table update");
1608 $this->table_update($user);
1609 log_main("chatt_send post table update");
1611 } // if ($update_room ...
1614 } // function chatt_send( ...
1616 function get_user($sess, &$idx)
1620 if (validate_sess($sess)) {
1621 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1622 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1625 $ret = $this->user[$i];
1629 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1630 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
1631 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1634 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1641 * function add_user(&$room, &$sess, &$idx, $name, $pass, $ip)
1644 * if ($idx > -1 && ret == FALSE) => duplicated nick
1645 * if ($idx == -2 && ret == FALSE) => invalid name
1646 * if ($idx == -3 && ret == FALSE) => wrong password
1647 * if ($idx == -1 && ret == FALSE) => no space left
1648 * if ($idx == 0 && ret == user) => SUCCESS
1649 * if ($idx == -$idx && ret == user) => SUCCESS (but the login exists in the auth db)
1652 function add_user(&$sess, &$idx, $name, $pass, $ip, $cookie)
1658 $authenticate = FALSE;
1660 $login_exists = FALSE;
1662 $ghost_auth = FALSE;
1667 if (($name_new = validate_name($name)) == FALSE) {
1672 log_auth("XXX", sprintf("ARRIVA: [%s] pass:[%s]", $sess, ($pass == FALSE ? "FALSE" : $pass)));
1673 if (validate_sess($sess) == FALSE)
1676 /* if pass != FALSE verify the login with pass */
1677 log_auth("XXX", "auth1");
1679 if (($bdb = BriskDB::create()) != FALSE) {
1681 if ($pass != FALSE) { // TODO: here add a method to $bdb to check if the db is available.
1682 log_auth("XXX", "auth2");
1683 $authenticate = $bdb->login_verify($name_new, $pass, $code);
1684 log_auth("XXX", "authenticate: ".($authenticate != FALSE ? "TRUE" : "FALSE"));
1686 if ($authenticate != FALSE) {
1687 $user_type = $authenticate->type_get();
1695 $login_exists = $bdb->login_exists($name_new);
1699 // if db is down, send a warning and verify only current users
1700 // no actions at this moment
1702 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1704 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1708 if ($idfree == -1 && strcmp($this->user[$i]->sess, "") == 0) {
1710 continue; // NOTE: CHECK IT !!
1712 if (strcasecmp($this->user[$i]->name, $name_new) == 0) {
1713 if ($authenticate != FALSE) {
1715 $ghost_auth = ($this->user[$i]->flags & USER_FLAG_AUTH);
1726 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1728 /* there is another user logged with your account and you and him have authenticated => new user
1729 get the session of the old user */
1730 if ($ghost > -1 && $ghost_auth && ($authenticate != FALSE)) {
1733 $ghost_user = $this->user[$ghost];
1735 $ghost_user->comm[$ghost_user->step % COMM_N] = "";
1736 $ghost_user->step_inc();
1739 $ghost_user->sess = $sess;
1742 $ghost_user->sess = $sess;
1745 // If user at the table we need to update the table data too
1746 $table_idx = $ghost_user->table;
1747 if ($ghost_user->stat == "table" && $this->table[$table_idx]->player_n == PLAYERS_N) {
1748 require_once("${G_base}briskin5/Obj/briskin5.phh");
1749 if (isset($this->match[$table_idx])) {
1750 $bri = $this->match[$table_idx];
1752 if ($bri->the_end != TRUE) {
1753 $bri->user[$ghost_user->table_pos]->comm[$bri->user[$ghost_user->table_pos]->step % COMM_N] = "";
1754 $bri->user[$ghost_user->table_pos]->step_inc();
1755 $bri->user[$ghost_user->table_pos]->sess = $sess;
1761 return ($this->user[$ghost]);
1763 else if ($idx != -1 && $i == MAX_PLAYERS) {
1768 $this->user[$idx]->sess = $sess;
1771 $this->user[$idx]->sess = $sess;
1773 $this->user[$idx]->name = $name_new; // OK - add new user
1774 $this->user[$idx]->stat_set("room");
1775 $this->user[$idx]->step_set(0);
1776 while (array_pop($this->user[$idx]->comm) != NULL);
1777 $this->user[$idx]->subst = "standup";
1778 $this->user[$idx]->lacc = $curtime;
1779 $this->user[$idx]->laccwr = $curtime;
1780 $this->user[$idx]->bantime = 0;
1781 $this->user[$idx]->ip = $ip;
1783 $this->user[$idx]->flags = $user_type;
1784 $this->user[$idx]->flags |= ($authenticate != FALSE ? USER_FLAG_AUTH : 0x00);
1785 $this->user[$idx]->flags |= ( ($pass != FALSE && $bdb == FALSE) ? USER_FLAG_DBFAILED : 0x00);
1786 log_auth("XXX", sprintf("FLAGS: [%x]", $this->user[$idx]->flags));
1789 if ($authenticate != FALSE) {
1790 $this->user[$idx]->code = $authenticate->code_get();
1791 $this->user[$idx]->flags |= USER_FLAG_LISTAUTH;
1793 if (isset($cookie['CO_list'])) {
1794 if (strcmp($cookie['CO_list'], "auth") == 0) {
1795 $this->user[$idx]->flags &= ~USER_FLAG_MAP_AUTH;
1796 $this->user[$idx]->flags |= USER_FLAG_LISTAUTH;
1798 if (strcmp($cookie['CO_list'], "isolation") == 0) {
1799 $this->user[$idx]->flags &= ~USER_FLAG_MAP_AUTH;
1800 $this->user[$idx]->flags |= USER_FLAG_ISOLAUTH;
1803 $this->user[$idx]->flags &= ~USER_FLAG_MAP_AUTH;
1809 log_main("ghost: rename!");
1810 $ghost_user = $this->user[$ghost];
1812 if ($ghost_auth == FALSE) {
1813 for ($sfx = 1 ; $sfx <= MAX_PLAYERS ; $sfx++) {
1814 $ghostname = 'ghost'.$sfx;
1815 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1816 if (strcmp("", $this->user[$i]->sess) == 0)
1819 if (strcasecmp($this->user[$i]->name, $ghostname) == 0) {
1824 if ($ghostname != '')
1828 $ghost_user->name = $ghostname;
1830 if ($ghost_user->stat == 'room' && $ghost_user->subst == 'standup') {
1831 $this->standup_update($ghost_user);
1834 log_main("chatt_send pre table update");
1835 $this->table_update($ghost_user);
1836 log_main("chatt_send post table update");
1838 } // if ($ghost_auth == FALSE
1840 // FIXME: cacciare il vecchio utente room && table (if needed)
1841 $ghost_user->the_end = TRUE;
1842 $ghost_user->lacc = 0;
1843 $this->garbage_manager(TRUE);
1845 } // if ($ghost > -1) {
1850 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));
1852 $ret = $this->user[$real_idx];
1859 function standup_update($user)
1861 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1862 $user_cur = $this->user[$i];
1863 if ($user_cur->sess == '')
1866 log_main("STANDUP START: ".$user_cur->stat);
1868 if ($user_cur->stat == 'room') {
1869 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".$this->standup_content($user_cur);
1870 if ($user->idx_get() == $i) {
1871 $user_cur->comm[$user_cur->step % COMM_N] .= $user->myname_innerHTML();
1873 log_main("FROM STANDUP: NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
1875 $user_cur->step_inc();
1881 static function create($crystal_filename)
1883 if (($room_ser = @file_get_contents($crystal_filename)) == FALSE ||
1884 ($room = unserialize($room_ser)) == FALSE) {
1885 fprintf(STDERR, "NEW ROOM\n");
1886 $room = new Room($crystal_filename);
1889 fprintf(STDERR, "ROOM FROM FILE\n");
1896 function load_data()
1901 if (($tok = @ftok(FTOK_PATH."/main", "C")) == -1) {
1902 log_main("ftok failed");
1906 if (($shm_sz = sharedmem_sz($tok)) == -1) {
1907 log_main("shmop_open failed");
1911 $shm_sz = SHM_DIMS_MIN;
1913 if ($shm = shm_attach($tok, $shm_sz)) {
1914 $room = @shm_get_var($shm, $tok); // CHECKED BELOW
1916 log_only("bri == ".($room == FALSE ? "FALSE" : "TRUE")." bri === ".($room === FALSE ? "FALSE" : "TRUE")." bri isset ".(isset($room) ? "TRUE" : "FALSE"));
1918 log_only("bri count ".count($room));
1920 if ($room == FALSE) {
1921 log_only("INIT MAIN DATA");
1924 $room = Room::create();
1926 log_shme("Room::create");
1928 if (Room::save_data($room) == FALSE)
1933 $room->shm_sz = $shm_sz;
1937 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1938 if (($room->user[$i] = User::load_data($i, FALSE)) == FALSE) {
1939 log_crit("User::load_data failed");
1943 if ($i < MAX_PLAYERS) {
1949 // SHSPLIT: load users from the shared memory
1958 function save_data_orig($room)
1966 if (($tok = @ftok(FTOK_PATH."/main", "C")) == -1)
1969 while ($room->shm_sz < SHM_DIMS_MAX) {
1970 if (($shm = shm_attach($tok, $room->shm_sz)) == FALSE)
1973 // log_only("PUT_VAR DI ".strlen(serialize($room)));
1974 if (@shm_put_var($shm, $tok, $room) != FALSE) {
1978 if (shm_remove($shm) === FALSE) {
1979 log_only("REMOVE FALLITA");
1983 $room->shm_sz += SHM_DIMS_DLT;
1991 function dump_data()
1993 $room_ser = serialize($this);
1994 $room_ser_len = mb_strlen($room_ser, "ASCII");
1995 if (file_put_contents($this->crystal_filename, $room_ser) == $room_ser_len) {
2002 function save_data($room)
2009 if (($tok = @ftok(FTOK_PATH."/main", "C")) == -1)
2012 // SHSPLIT: before save the $room you must save users,
2013 // detach from main struct and (then) reattach
2014 $user_park = array();
2015 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
2016 $user_park[$i] = $room->user[$i];
2017 $room->user[$i] = FALSE;
2020 while ($room->shm_sz < SHM_DIMS_MAX) {
2021 if (($shm = shm_attach($tok, $room->shm_sz)) == FALSE)
2024 // log_only("PUT_VAR DI ".strlen(serialize($room)));
2025 if (@shm_put_var($shm, $tok, $room) != FALSE) {
2026 log_shme("Room::save_data");
2030 if (shm_remove($shm) === FALSE) {
2031 log_only("REMOVE FALLITA");
2035 $room->shm_sz += SHM_DIMS_DLT;
2041 // SHSPLIT: reattach users to the room class
2042 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
2043 User::save_data($user_park[$i], $i);
2044 $room->user[$i] = $user_park[$i];
2046 log_load("FINISH: ".($ret == TRUE ? "TRUE" : "FALSE"));
2051 static function lock_data($is_exclusive)
2053 if (($res = file_lock(FTOK_PATH."/main", $is_exclusive)) != FALSE) {
2054 self::$delta_t = microtime(TRUE);
2055 log_lock("LOCK room [".self::$delta_t."]");
2063 static function unlock_data($res)
2067 log_lock("UNLOCK room [".(microtime(TRUE) - (self::$delta_t))."]");
2073 function standup_content($user)
2078 if ($user->stat != 'room')
2081 for ($i = 0 , $ct = 0 ; $ct < 4 && $i < MAX_PLAYERS ; $i++) {
2082 if ($this->user[$i]->sess == "" || $this->user[$i]->stat != "room" || $this->user[$i]->name == "")
2087 // $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
2089 $content = ' j_stand_cont( [ ';
2091 for ($i = 0 , $ct = 0 ; $i < MAX_PLAYERS ; $i++) {
2092 if ($this->user[$i]->sess == "" || $this->user[$i]->stat != "room" || $this->user[$i]->name == "")
2095 $flags = $this->user[$i]->flags;
2097 if ($this->user[$i]->subst == "standup") {
2098 if ($user->idx_get() == $i) {
2102 $content .= sprintf('%s[ %d, "%s" ]',($ct > 0 ? ', ' : ''), $flags, xcape($this->user[$i]->name));
2111 function table_content($user, $table_idx)
2117 // Si possono usare i dati nella classe table
2120 $sess = $user->sess;
2121 $table = $this->table[$table_idx];
2123 if ($user->stat != 'room')
2127 for ($i = 0 ; $i < $table->player_n ; $i++) {
2128 $user_cur = $this->user[$table->player[$i]];
2130 $flags = $user_cur->flags;
2132 if ($user->idx_get() == $table->player[$i])
2135 log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
2137 $content .= sprintf('%s[ %d, "%s" ]',($i == 0 ? '' : ', '), $flags, xcape($user_cur->name));
2142 $ret .= sprintf('j_tab_cont(%d, %s);', $table_idx, $content);
2147 function request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
2149 printf("NEW_SOCKET (root): %d\n", intval($new_socket));
2151 $enc = get_encoding($header);
2152 if (isset($header['User-Agent'])) {
2153 if (strstr($header['User-Agent'], "MSIE")) {
2154 $transp_type = "htmlfile";
2157 $transp_type = "xhr";
2161 $transp_type = "iframe";
2163 force_no_cache($header_out);
2169 index_main($this, $transp_type, $header_out, $addr, $get, $post, $cookie);
2170 $content = ob_get_contents();
2173 $s_a_p->pgflush_try_add($enc, $new_socket, 20, $header_out, $content);
2177 case "index_wr.php":
2179 index_wr_main($this, $addr, $get, $post, $cookie);
2180 $content = ob_get_contents();
2183 $s_a_p->pgflush_try_add($enc, $new_socket, 20, $header_out, $content);
2187 case "index_rd_ifra.php":
2189 if (!isset($cookie['sess'])
2190 || (($user = $this->get_user($cookie['sess'], $idx)) == FALSE)) {
2191 $content = User::stream_fini($s_a_p->rndstr, TRUE);
2193 $s_a_p->pgflush_try_add($enc, $new_socket, 20, $header_out, $content);
2198 // close a previous opened index_read_ifra socket, if exists
2199 if (($prev = $user->rd_socket_get()) != NULL) {
2200 $s_a_p->socks_unset($user->rd_socket_get());
2201 fclose($user->rd_socket_get());
2202 printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
2203 $user->rd_socket_set(NULL);
2207 $user->stream_init($s_a_p->rndstr, $enc, $header_out, $content, $get, $post, $cookie);
2209 $response = headers_render($header_out, -1).chunked_content($user->rd_zls_get(), $content);
2210 $response_l = mb_strlen($response, "ASCII");
2212 $wret = @fwrite($new_socket, $response, $response_l);
2213 if ($wret < $response_l) {
2214 printf("TROUBLES WITH FWRITE: %d\n", $wret);
2215 $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
2218 $user->rd_cache_set("");
2220 fflush($new_socket);
2223 $s_a_p->socks_set($new_socket, $user);
2224 $user->rd_socket_set($new_socket);
2225 printf(" - qui ci siamo - ");
2233 /* FAR TODO: move all into an array of registered sub-apps */
2234 $subs = "briskin5/";
2235 $subs_l = strlen($subs);
2236 if (!strncmp($path, $subs, $subs_l)) {
2237 $ret = Bin5::request_mgr(&$s_a_p, $header, &$header_out, &$new_socket, substr($path, $subs_l) , $addr, $get, $post, $cookie);
2246 function match_add($idx, $match)
2248 $this->match[$idx] = $match;
2251 function match_del($idx)
2253 unset($this->match[$idx]);
2256 function match_get($idx, $token)
2258 if (isset($this->match[$idx])) {
2260 || $token == $this->match[$idx]->table_token) {
2261 return ($this->match[$idx]);
2269 function make_seed()
2271 list($usec, $sec) = explode(' ', microtime());
2272 return (float) $sec + ((float) $usec * 100000);
2275 function btrace_line($ar)
2277 GLOBAL $G_btrace_pref_sub;
2280 for ($i = 0 ; $i < count($ar) ; $i++) {
2281 $with_class = isset($ar[$i]['class']);
2282 $with_file = isset($ar[$i]['file']);
2283 $ret .= sprintf("%s%s%s (%s:%d)", ($i == 0 ? "" : ", "),
2284 ($with_class ? $ar[$i]['class'].$ar[$i]['type'] : ""),
2285 $ar[$i]['function'], ($with_file ? str_replace($G_btrace_pref_sub, "", $ar[$i]['file']) : ""),
2286 ($with_file ? $ar[$i]['line'] : ""));
2292 function trace_ftok($id, $add)
2294 // NOTE: without space to use sed to substitute "= @ftok(" with "= @ftok("
2295 $tok=@ftok($id, $add);
2297 log_shme($tok.": ".$id." + ".$add);
2302 function log_mop($step, $log)
2304 GLOBAL $sess, $PHP_SELF;
2306 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LMOP) == 0)
2309 if (isset($sess) == FALSE)
2314 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LMOP) == 0)
2317 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2318 $btrace = btrace_line(debug_backtrace());
2321 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2322 fwrite($fp, sprintf("LMOP: [%f] [%05d] [%s] [%s]\n", gettimeofday(TRUE), $step, $log, $btrace));
2328 function log_only2($log)
2330 GLOBAL $sess, $PHP_SELF;
2332 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONL2) == 0)
2335 if (isset($sess) == FALSE)
2340 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONL2) == 0)
2343 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2344 $btrace = btrace_line(debug_backtrace());
2347 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2348 fwrite($fp, sprintf("ONL2: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2353 function log_crit($log)
2355 GLOBAL $sess, $PHP_SELF;
2357 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_CRIT) == 0)
2360 if (isset($sess) == FALSE)
2365 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_CRIT) == 0)
2368 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2369 $btrace = btrace_line(debug_backtrace());
2372 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2373 fwrite($fp, sprintf("CRIT: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2378 function log_only($log)
2380 GLOBAL $sess, $PHP_SELF;
2382 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONLY) == 0)
2385 if (isset($sess) == FALSE)
2390 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONLY) == 0)
2393 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2394 $btrace = btrace_line(debug_backtrace());
2397 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2398 fwrite($fp, sprintf("ONLY: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2403 function log_main($log)
2405 GLOBAL $sess, $PHP_SELF;
2407 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_MAIN) == 0)
2410 if (isset($sess) == FALSE)
2415 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_MAIN) == 0)
2418 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2419 $btrace = btrace_line(debug_backtrace());
2422 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2423 fwrite($fp, sprintf("MAIN: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2428 function log_rd($log)
2430 GLOBAL $sess, $PHP_SELF;
2432 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_READ) == 0)
2435 if (isset($sess) == FALSE)
2440 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_READ) == 0)
2443 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2444 $btrace = btrace_line(debug_backtrace());
2447 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2448 fwrite($fp, sprintf("READ: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2453 function log_rd2($log)
2455 GLOBAL $sess, $PHP_SELF;
2457 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_REA2) == 0)
2460 if (isset($sess) == FALSE)
2465 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_REA2) == 0)
2468 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2469 $btrace = btrace_line(debug_backtrace());
2473 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2474 fwrite($fp, sprintf("REA2: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2479 function log_send($log)
2481 GLOBAL $sess, $PHP_SELF;
2483 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SEND) == 0)
2486 if (isset($sess) == FALSE)
2491 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SEND) == 0)
2494 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2495 $btrace = btrace_line(debug_backtrace());
2498 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2499 fwrite($fp, sprintf("SEND: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2504 function log_lock($log)
2506 GLOBAL $sess, $PHP_SELF;
2508 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOCK) == 0)
2511 if (isset($sess) == FALSE)
2516 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOCK) == 0)
2519 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2520 $btrace = btrace_line(debug_backtrace());
2523 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2524 fwrite($fp, sprintf("LOCK: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2529 function log_wr($log)
2531 GLOBAL $sess, $PHP_SELF;
2533 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_WRIT) == 0)
2536 if (isset($sess) == FALSE)
2541 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_WRIT) == 0)
2544 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2545 $btrace = btrace_line(debug_backtrace());
2548 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2549 fwrite($fp, sprintf("WRIT: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2554 function log_load($log)
2556 GLOBAL $sess, $PHP_SELF;
2558 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOAD) == 0)
2561 if (isset($sess) == FALSE)
2566 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOAD) == 0)
2569 if ((BRISK_DEBUG | ($ssess == 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] [%s] [%s]\n", $ssess, $log, $btrace));
2579 function log_auth($sess, $log)
2583 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_AUTH) == 0)
2586 if (( (BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_AUTH) == 0)
2589 if ((BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2590 $btrace = btrace_line(debug_backtrace());
2593 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2594 fwrite($fp, sprintf("LOAD: [%s] [%d] [%s] [%s]\n", $sess, time(), $log, $btrace));
2599 function log_shme($log)
2601 GLOBAL $sess, $PHP_SELF;
2603 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SHME) == 0)
2606 if (isset($sess) == FALSE)
2611 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SHME) == 0)
2614 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2615 $btrace = btrace_line(debug_backtrace());
2618 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2619 fwrite($fp, sprintf("SHME: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2626 // function log_legal($curtime, $sess, $name, $where, $mesg)
2627 function log_legal($curtime, $addr, $user, $where, $mesg)
2630 if (($fp = @fopen(LEGAL_PATH."/legal.log", 'a')) != FALSE) {
2631 /* Unix time | session | nickname | IP | where was | mesg */
2632 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
2633 ($user->flags & USER_FLAG_AUTH ? 'A' : 'N'),
2634 $user->name, $addr, $where , $mesg));
2639 function table_act_content($isstanding, $sitted, $table, $cur_table, $allowed)
2644 if ($sitted < PLAYERS_N) {
2652 if ($table == $cur_table)
2659 $ret = sprintf('j_tab_act_cont(%d, \'%s\');', $table, $act);
2664 function show_notify($text, $tout, $butt, $w, $h)
2666 log_main("SHOW_NOTIFY: ".$text);
2667 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
2670 function show_notify_ex($text, $tout, $butt, $w, $h, $is_opaque, $block_time)
2672 log_main("SHOW_NOTIFY OPAQUE: ".$text);
2673 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);
2677 function root_wellcome($user)
2679 GLOBAL $root_wellarr, $G_lang;
2683 $dt = date("H:i ", $curtime);
2685 for ($i = 0 ; $i < count($root_wellarr[$G_lang]) ; $i++)
2686 $ret .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, str_replace('"', '\"', $root_wellarr[$G_lang][$i]));
2693 function validate_sess($sess)
2695 if (strlen($sess) == SESS_LEN)
2701 function validate_name($name)
2703 $name_new = str_replace(' ', '_', mb_substr(trim($name),0,12, "UTF-8"));
2705 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
2707 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
2714 function playsound($filename)
2716 return (sprintf('playsound("flasou", "%s");', $filename));
2719 function secstoword($secs)
2725 $mins = floor($secs / 60);
2727 if ($G_lang == 'en') {
2729 $ret = sprintf("%d minute%s%s", $mins, ($mins > 1 ? "s" : ""), ($secs > 0 ? " and " : ""));
2732 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "s" : ""));
2736 $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
2739 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));
2744 function sharedmem_sz($tok)
2746 if (($shm_id = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
2747 log_main("shmop_open failed");
2750 $shm_sz = shmop_size($shm_id);
2751 shmop_close($shm_id);
2753 // log_main("shm_sz: ".$shm_sz." SHM_DIMS: ".SHM_DIMS);
2760 static function lock_data($is_exclusive)
2762 if (($res = file_lock(FTOK_PATH."/warrant", $is_exclusive)) != FALSE) {
2763 self::$delta_t = microtime(TRUE);
2764 log_lock("LOCK warrant [".self::$delta_t."]");
2772 static function unlock_data($res)
2776 log_lock("UNLOCK warrant [".(microtime(TRUE) - (self::$delta_t))."]");
2785 static function lock_data($is_exclusive)
2787 if (($res = file_lock(FTOK_PATH."/poll", $is_exclusive)) != FALSE) {
2788 self::$delta_t = microtime(TRUE);
2789 log_lock("LOCK poll [".self::$delta_t."]");
2797 static function unlock_data($res)
2801 log_lock("UNLOCK poll [".(microtime(TRUE) - (self::$delta_t))."]");