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);
48 define('CHAT_ENABLED', TRUE);
50 define('SESS_LEN', 13);
51 define('STREAM_TIMEOUT', 60);
52 /* FIXME: move to sac-a-push .phh */
53 define('EXPIRE_TIME_RD', 180);
54 define('EXPIRE_TIME_SMAMMA', 360);
55 define('EXPIRE_TIME_WAG', 10);
56 define('WAKEUP_TIME', 12);
57 // BAN_TIME da allineare anche in commons.js
58 define('BAN_TIME', 3600);
59 define('GARBAGE_TIMEOUT', 10);
60 define('NICKSERV', "<i>BriskServ</i>");
62 define('LOCK_SHARE_MAX', 10000);
64 define('DBG_ONL2', 0x0001);
65 define('DBG_ONLY', 0x0002);
66 define('DBG_MAIN', 0x0004);
67 define('DBG_READ', 0x0008);
68 define('DBG_REA2', 0x0010);
69 define('DBG_SEND', 0x0020);
70 define('DBG_LOCK', 0x0040);
71 define('DBG_WRIT', 0x0080);
72 define('DBG_LOAD', 0x0100);
73 define('DBG_AUTH', 0x0200);
74 define('DBG_CRIT', 0x0400);
75 define('DBG_LMOP', 0x0800);
76 define('DBG_TRAC', 0x1000);
77 define('DBG_SHME', 0x2000);
78 // NOTE: BRISK DEBUG must be a numerical constant, not the result of operations on symbols
79 define('BRISK_DEBUG', 0x0800);
81 define('BRISK_SINGLE_DEBUG',0);
82 define('BRISK_SINGLE_SESS', "");
83 define('DEBUGGING', "no-debugging");
85 require_once("$DOCUMENT_ROOT/Etc/".BRISK_CONF);
87 $mlang_brisk = array( 'btn_backstand'=> array( 'it' => 'torna in piedi',
88 'en' => 'back standing' ),
89 'btn_close' => array( 'it' => 'chiudi',
92 'tit_all' => array( 'it' => 'tutti',
95 'tabtout_a'=> array( 'it' => '<br>Sei stato inattivo per ',
96 'en' => '<br>You are being idle for ' ),
97 'tabtout_b'=> array( 'it' => ' minuti. <br><br>Quindi ritorni tra i <b>Giocatori in piedi</b>.',
98 'en' => ' minutes. <br><br>Then you return with the <b>standing players</b>.'),
99 'tickmust' => array( 'it' => '<br>Per attivare il messaggio di segnalazione del tavolo occorre essere seduti.<br><br>',
100 'en' => '<br>To activate the signalling message of the table it\'s necessary to be sitting<br><br>'),
101 'tickjust' => array( 'it' => '<br>Il messaggio di segnalazione del tavolo è già attivato.<br><br> ',
102 'en' => 'EN<br>Il messaggio di segnalazione del tavolo è già attivato.<br><br> '),
103 'tickwait' => array( 'it' => '<br>Il messaggio di segnalazione del tavolo<br>è disattivato ancora per %d second%s.<br><br>',
104 'en' => 'EN<br>The signalling message of the table<br>will be deactivated for %d second%s.<br><br>'),
105 'alarpass' => array( 'it' => '<br>La password digitata non è corretta.<br><br>',
106 'en' => '<br>The entered password is not correct.<br><br>'),
107 'alarret' => array( 'it' => '"Alarm \\"<b>%s</b>\\" inviato a <b>%s</b>."',
108 'en' => '"Alarm \\"<b>%s</b>\\" sent to <b>%s</b>."'),
109 'authmust' => array( 'it' => '<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>',
110 'en' => '<b>To authenticate someone you have to be authenticated.</b>'), // on your turn
111 'mesgmust' => array( 'it' => '<b>Per inviare un messaggio devi essere autenticato.</b>',
112 'en' => '<b>To send a message you have to be authenticated.</b>'),
113 'nickmust' => array( 'it' => 'Il nickname deve contenere almeno una lettera dell\'alfabeto o una cifra.',
114 'en' => 'The nickname have to contain at least one letter or one number.'),
115 'nickdupl' => array( 'it' => 'Nickname <b>%s</b> già in uso.',
116 'en' => 'The nickname <b>%s</b> is already in use.'),
117 'authchan' => array( 'it' => '<b>Non puoi cambiare nick a un tavolo per soli autenticati o se sei in modalità isolata.</b>',
118 'en' => '<b>You can\'t change your nickname into a table for only authenticated or if you are in isolation mode.</b>'),
119 '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>',
120 '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 ???
121 'statunkn' => array( 'it' => 'Questo stato non esiste.',
122 'en' => 'This state don\'t exists.'),
123 'tabincon' => array( 'it' => '<br>I dati del tavolo n° %d sono inconsistenti, verranno resettati.<br><br>Torni in piedi.<br><br>',
124 'en' => 'EN <br>I dati del tavolo n° %d sono inconsistenti, verranno resettati.<br><br>Torni in piedi.<br><br>'),
125 'listmust' => array( 'it' => '<b>Per andare in isolamento non bisogna essere seduti a tavoli non riservati.</b>',
126 'en' => '<b>To go to isolation you must don\'t stay on not reserved tables</b>'),
128 'tit_onauth'=>array( 'it' => '(solo aut.)',
129 'en' => '(only aut.)'),
130 'tit_onisol'=>array( 'it' => '(isolam.to)',
131 'en' => '(isolation)'),
132 '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.',
133 'en' => 'Connection to the database failed<br>All authentications are suspended temporarly, you login as normal user.<br>We are about the limitation')
138 $G_lng = langtolng($G_lang);
140 $G_all_points = array( 11,10,4,3,2, 0,0,0,0,0 );
141 $G_brisk_version = "3.6.3";
143 /* MLANG: ALL THE INFO STRINGS IN brisk.phh */
144 $root_wellarr = array( 'it' => array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NOVITA\'</b>: nuovo sistema di evidenziazione degli utenti registrati.',
145 'Se vuoi iscriverti alla <a target="_blank" href="mailto:ml-briscola+subscribe@milug.org">Mailing List</a>, cliccala!' ),
146 'en' => array ( 'Brisk (Ver. '.$G_brisk_version.'), <b>NEWS</b>: usage of reader/writer locking instead of generic exclusive locking.',
147 'If you want to subscribe our <a target="_blank" href="ml-briscola+subscribe@milug.org">Mailing List</a>, click it!' ) );
149 $G_room_help = array( 'it' => '
150 <div style=\\"text-align: left; padding: 8px;\\">
151 <b>Descrizione</b><br>
152 Questa è un\'implementazione della briscola in cinque, così come è spiegata su
153 <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>
154 <b>Configurazione del browser.</b><br>
155 Occorre abilitare i cookies.<br>
157 <b>Uso del sito</b><br>
158 Potete sedervi a un tavolo o rimanere in piedi.<br>
159 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
162 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>
163 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
164 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
165 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
166 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
167 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.
169 <dt><b>Comandi della chat</b>
170 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
171 <dd><b>/tav <i><frase di invito></i></b> - invito per gli altri giocatori al tavolo dove si è seduti
172 <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\\"
173 <dd><b>/authreq</b> - se si è autenticati permette di garantire per un utente fidato
174 <dd><b>/mesgtoadm</b> - se si è autenticati permette di lasciare un messaggio all\'amministratore del sito
175 <dd><b>/listen <all or auth></b> - se si è autenticati permette leggere solo i messaggi degli altri autenticati (auth) o di tutti (all)
181 <div style=\\"text-align: left; padding: 8px;\\">
182 <b>EN Descrizione</b><br>
183 EN Questa è un\'implementazione della briscola in cinque, così come è spiegata su
184 <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>
185 <b>EN Configurazione del browser.</b><br>
186 Occorre abilitare i cookies.<br>
188 <b>Uso del sito</b><br>
189 Potete sedervi a un tavolo o rimanere in piedi.<br>
190 Se al vostro tavolo si raggiungono i 5 giocatori inizia automaticamente la partita.<br>
193 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>
194 Chi vince l\'asta dovrà decidere il seme della carta scelta e inizierà la mano.<br>
195 Per giocare le carte dovrete trascinarle nel quadrato al centro del vostro schermo.<br><br>
196 Il vostro turno è sempre segnalato da una cornice verde lampeggiante intorno al quadrato al centro del vostro schermo.<br><br>
197 Durante la partita, se vorrete ricaricare la pagina, usate l\'apposito bottone \\"reload\\" in basso a destra.<br>
198 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.
200 <dt><b>Comandi della chat</b>
201 <dd><b>/nick <i><nuovo_nickname></i></b> - cambio di nickname
202 <dd><b>/tav <i><frase di invito></i></b> - invito per gli altri giocatori al tavolo dove si è seduti
203 <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\\"
204 <dd><b>/authreq</b> - se si è autenticati permette di garantire per un utente fidato
205 <dd><b>/mesgtoadm</b> - se si è autenticati permette di lasciare un messaggio all\'amministratore del sito
206 <dd><b>/listen <all or auth></b> - se si è autenticati permette leggere solo i messaggi degli altri autenticati (auth) o di tutti (all)
212 $G_room_passwdhowto = array( 'it' => '<br><h2>Come registrarsi su Brisk</h2>
213 <div style=\\"text-align: left; padding: 8px;\\">
214 Attualmente ci sono due metodi per ottenere una password sul sito:<br><br>
216 <li><b>Facendosi garantire da un utente di Brisk che già possidede una password</b><br><br>
217 <li><b>Auto-garantendosi utilizzando uno dei seguenti sistemi di identificazione digitale:</b><br><br>
219 <li>Carta Regionale dei Servizi della Lombardia (la tessera sanitaria)
220 <li>Carta Regionale dei Servizi del Friuli Venezia Giulia (la tessera sanitaria)
221 <li>Smart card di InfoCamere
224 <b>Per auto-garantisi occorre possedere:</b><br><br>
226 <li>il codice PIN della propria carta
227 <li>il lettore di smart-card per collegare la carta al PC (acquistabile di solito presso le edicole)
230 <b>Per effettuare la registrazione collegarsi al sito:</b><br><br>
232 <dd><a class=\\"flat\\" target=\\"_blank\\" href=\\"https://brisk.mine.nu\\">https://brisk.mine.nu</a>
235 Se sei in possesso di una carta che permette l\'identificazione via internet che non è nell\'elenco qui sopra
236 <a class=\\"flat\\" href=\\"mailto:authadmbrisk@alternativeoutput.it\\">fai una segnalazione</a>.
241 'en' => '<br><h2>EN Come registrarsi su Brisk</h2>
242 <div style=\\"text-align: left; padding: 8px;\\">
243 EN Attualmente ci sono due metodi per ottenere una password sul sito:<br><br>
245 <li><b>Facendosi garantire da un utente di Brisk che già possidede una password</b><br><br>
246 <li><b>Auto-garantendosi utilizzando uno dei seguenti sistemi di identificazione digitale:</b><br><br>
248 <li>Carta Regionale dei Servizi della Lombardia (la tessera sanitaria)
249 <li>Carta Regionale dei Servizi del Friuli Venezia Giulia (la tessera sanitaria)
252 <b>Per auto-garantisi occorre possedere:</b><br><br>
254 <li>il codice PIN della propria carta
255 <li>il lettore di smart-card per collegare la carta al PC (acquistabile di solito presso le edicole)
258 <b>Per effettuare la registrazione collegarsi al sito:</b><br><br>
260 <dd><a class=\\"flat\\" target=\\"_blank\\" href=\\"https://brisk.mine.nu\\">https://brisk.mine.nu</a>
263 Se sei in possesso di una carta che permette l\'identificazione via internet che non è nell\'elenco qui sopra
264 <a class=\\"flat\\" href=\\"mailto:authadmbrisk@alternativeoutput.it\\">fai una segnalazione</a>.
270 <dd>Seguendo la procedura di auto-garanzia all\'url: <a href="https://brisk.mine.nu">https://brisk.mine.nu</a>
274 $G_room_about = array( 'it' => '<br>
275 <div id=\\"header\\" class=\\"header\\">
276 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
277 briscola chiamata in salsa ajax
279 <br><b>version '.$G_brisk_version.'</b><br><br>
280 Copyright 2006-2012 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>',
282 <div id=\\"header\\" class=\\"header\\">
283 <img class=\\"nobo\\" src=\\"img/brisk_logo64.png\\">
284 declaration briscola in ajax sauce <b>(Beta)</b>
286 <br><b>version '.$G_brisk_version.'</b><br><br>
287 Copyright 2006-2009 <a href=\\"mailto:brisk@alternativeoutput.it\\">Matteo Nastasi</a> (aka mop)<br><br>');
291 for ($i = 0; $i < ob_get_level(); $i++)
293 ob_implicit_flush(1);
297 function force_no_cache(&$header_out)
299 $header_out['Pragma'] = 'no-cache, must-revalidate';
300 $header_out['Cache-Control'] = 'no-cache';
301 $header_out['Expires'] = '-1';
304 function file_lock($fname, $is_exclusive)
306 if (($res = @fopen($fname, "r+")) == FALSE) {
310 if (flock($res, ($is_exclusive ? LOCK_EX : LOCK_SH)) == FALSE) {
318 function file_unlock($res)
321 flock($res, LOCK_UN);
326 function webservers_exceeded()
328 return(file_exists(PROXY_PATH."/webservers_exceded.flag"));
331 function webservers_check()
333 GLOBAL $G_webserver_max;
335 /* FIXME: check all procs expirations */
340 $dh = opendir('/proc');
341 while (($file = readdir($dh)) !== false) {
342 if (preg_match('/[0-9]+/', $file)) {
343 $cmdline = explode("\0", file_get_contents('/proc/'.$file.'/cmdline'));
344 // echo "xxx".$cmdline[0].$n;
345 if (strstr('/usr/sbin/apache2', $cmdline[0]) != FALSE) {
346 // echo "yyy".$cmdline[0].$n;
353 if ($ct >= $G_webserver_max) {
354 touch(PROXY_PATH."/webservers_exceded.flag");
357 unlink(PROXY_PATH."/webservers_exceded.flag");
362 $escpush_from = array("\\", "\"");
363 $escpush_to = array("\\\\", "\\\"");
366 GLOBAL $escpush_from, $escpush_to;
368 return str_replace($escpush_from, $escpush_to, $s);
371 $escinp_from = array( "\"" );
372 $escinp_to = array( """ );
374 function escinput($s)
376 GLOBAL $escinp_from, $escinp_to;
378 return str_replace($escinp_from, $escinp_to, $s);
383 return htmlentities($s);
386 function esclfhtml($s)
388 return str_replace("\n", "<br>\n", htmlentities($s));
392 function langtolng($lang)
396 return ($G_lang == 'en' ? '-en' : '');
399 function csplitter($in, $sep)
405 for ($i = 0 ; $i < strlen($in) ; $i++) {
406 $ini = substr($in, $i, 1);
410 else if ($ini == $sep) {
429 $from = array ( '\\', '@', '|' );
430 $to = array ( '\\\\', '@', '¦' );
432 return (str_replace($from, $to, htmlentities($s,ENT_COMPAT,"UTF-8")));
437 $from = array ( '\\', '|', "\t", "\n");
438 $to = array ( '\\\\', '\\|', "\\t", "\\n");
440 return (str_replace($from, $to, $s));
443 function xcapemesg($s)
445 $from = array ( "\n");
446 $to = array ( "\\n");
448 return (str_replace($from, $to, $s));
458 function getbyid($idx)
460 return ($this->el[$idx]);
463 function setbyid($idx, $v)
465 $this->el[$idx] = $v;
474 var $auth_only; // se tavolo riservato o libero
481 var $table_start; // information field
489 function create($idx)
491 if (($thiz = new Table()) == FALSE)
495 $thiz->player = array();
497 $thiz->auth_only = FALSE;
503 $thiz->table_token = "";
504 $thiz->table_start = 0;
506 $thiz->wakeup_time = 0;
513 $this->idx = $from->idx;
514 $this->player = array();
515 for ($i = 0 ; $i < $from->player_n ; $i++)
516 $this->player[$i] = $from->player[$i];
517 $this->player_n = $from->player_n;
519 log_main("PLAYER_N - parent::copy.".$this->player_n);
521 $this->auth_only = $from->auth_only;
523 $this->wag_own = $from->wag_own;
524 $this->wag_com = $from->wag_com;
525 $this->wag_tout = $from->wag_tout;
527 $this->table_token = $from->table_token;
528 $this->table_start = $from->table_start;
530 $this->wakeup_time = $from->wakeup_time;
533 function myclone($from)
535 if (($thiz = new Table()) == FALSE)
543 function spawn($from)
545 if (($thiz = new Table()) == FALSE)
548 $thiz->idx = $from->idx;
549 $thiz->player = array();
550 for ($i = 0 ; $i < $from->player_n ; $i++)
551 $thiz->player[$i] = $i;
552 $thiz->player_n = $from->player_n;
554 $thiz->auth_only = $from->auth_only;
556 $thiz->wag_own = $from->wag_own;
557 $thiz->wag_com = $from->wag_com;
558 $thiz->wag_tout = $from->wag_tout;
560 $thiz->table_token = $from->table_token;
561 $thiz->table_start = $from->table_start;
563 $thiz->wakeup_time = $from->wakeup_time;
568 function wag_set($user_idx, $mesg)
572 $this->wag_own = $user_idx;
573 $this->wag_com = $mesg;
577 function wag_reset($timeout)
579 log_main("WAG_RESET");
581 unset($this->wag_own);
584 $this->wag_tout = $timeout;
587 function player_get($idx)
589 return ($this->player[$idx]);
592 function player_set($idx, $player)
594 $this->player[$idx] = $player;
597 function user_add($idx)
599 $this->player[$this->player_n] = $idx;
602 return ($this->player_n - 1);
605 function user_rem($room, $user)
607 $tabpos = $user->table_pos;
609 /* verifico la consistenza dei dati */
610 if ($room->user[$this->player[$tabpos]] == $user) {
612 /* aggiorna l'array dei giocatori al tavolo. */
613 for ($i = $tabpos ; $i < $this->player_n-1 ; $i++) {
614 $this->player[$i] = $this->player[$i+1];
615 $user_cur = $room->user[$this->player[$i]];
616 $user_cur->table_pos = $i;
621 log_main("INCONSISTENCY ON TABLE.");
627 // $ret .= table_act_content(($user->subst == 'standup'), $this->table[$i]->player_n, $i, $user->table,
628 // ($this->table[$i]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
630 // function act_content($isstanding, $sitted, $table, $cur_table, $allowed)
631 function act_content($user)
634 $isstanding = ($user->subst == 'standup');
635 $sitted = $this->player_n;
637 $cur_table = $user->table;
641 if ($sitted < PLAYERS_N) {
642 if ($this->auth_only) {
643 if ($user->flags & USER_FLAG_AUTH)
657 if ($table == $cur_table)
664 $ret = sprintf('j_tab_act_cont(%d, \'%s\');', $table, $act);
679 var $comm; // commands for many people
680 var $step; // current step of the comm array
681 // externalized var $garbage_timeout;
685 $this->user = array();
686 $this->table = array();
687 $this->match = array();
689 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
690 $this->user[$i] = User::create($this, $i, "", "");
693 for ($i = 0 ; $i < TABLES_N ; $i++) {
694 $this->table[$i] = Table::create($i);
697 $row = ( (((int)($i / 4)) % 2) == 0 );
698 $col = ($i % 2 == 0);
699 $this->table[$i]->auth_only = (($row && $col) || (!$row && !$col));
702 $this->table[$i]->auth_only = FALSE;
705 if ($i < TABLES_AUTH_N)
706 $this->table[$i]->auth_only = TRUE;
708 $this->table[$i]->auth_only = FALSE;
710 // externalized $this->garbage_timeout = 0;
711 Room::garbage_time_expire_set(0);
712 $this->shm_sz = SHM_DIMS_MIN;
715 function garbage_manager($force)
717 GLOBAL $G_lang, $mlang_brisk, $G_base;
721 log_rd2("garbage_manager START");
723 /* Garbage collector degli utenti in timeout */
726 // externalized if ($force || $this->garbage_timeout < $curtime) {
727 if (!$force && !Room::garbage_time_is_expired($curtime)) {
733 // Before all align times with table timeout
734 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
735 $table_cur = $this->table[$table_idx];
736 // if the table is complete and exists its shared mem we get the info about users lacc
738 if ($table_cur->player_n == PLAYERS_N) {
739 log_main("PLAYERS == N TABLE ".$table_idx);
742 $no_recovery = FALSE;
743 if (isset($this->match[$table_idx])) {
744 $bri = $this->match[$table_idx];
746 if ($table_cur->table_token != $bri->table_token) {
747 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
748 log_main("ERROR: not matching table_start. Room: ".$table_cur->table_start." Table: ".$bri->table_start);
758 log_main("garbage_manager: bri loaded successfully.");
759 $bri->garbage_manager(TRUE);
761 $bri_table = $bri->table[0];
763 // is the end of the table
765 if ($bri->the_end == TRUE) {
767 * DESTROY OF FINISHED TABLE && MOVE PLAYER TO ROOM AGAIN
769 log_main("garbage_manager: INSIDE THE END.");
771 $plist = "$table_cur->table_token|$table_cur->idx|$table_cur->player_n";
772 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
773 $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
776 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
777 // stat must be "table" by definition
778 $user_cur = $this->user[$table_cur->player[$i]];
779 $bri_user = $bri->user[$i];
781 $user_cur->subst = $bri_user->subst;
782 $user_cur->step = $bri_user->step;
783 $user_cur->lacc = $bri_user->lacc;
784 $user_cur->laccwr = $bri_user->lacc;
785 $user_cur->bantime = $bri_user->bantime;
788 log_legal($curtime, 'xxx', $user_cur, "STAT:DESTROY_GAME", $plist);
790 $this->room_join_wakeup($user_cur, FALSE, 0);
791 $table_cur->table_token = "";
792 $table_cur->wakeup_time = $curtime + WAKEUP_TIME;
794 $this->match_del($table_idx);
797 log_main("gm:: save_data");
799 for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
800 $this->user[$table_cur->player[$i]]->lacc = $bri->user[$i]->lacc;
803 } // if ($bri == FALSE
804 else if ($no_recovery == FALSE) {
805 log_crit("ERROR: table ".$table_idx." unrecoverable join");
807 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
808 $user_cur = $this->user[$table_cur->player[$i]];
809 $user_cur->subst = "shutdowner";
810 $user_cur->step_inc();
812 $ret = sprintf('stat = "%s"; subst = "%s";', $user_cur->stat, $user_cur->subst);
813 $ret .= "gst.st = ".($user_cur->step+1)."; ";
814 // MLANG <br>I dati del tavolo n° ".$user_cur->table." sono inconsistenti, verranno resettati.<br><br>Torni in piedi.<br><br>
815 $prestr = sprintf($mlang_brisk['tabincon'][$G_lang], $user_cur->table);
816 $ret .= show_notify($prestr, 2000, $mlang_brisk['btn_close'][$G_lang], 400, 110);
817 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
818 $user_cur->step_inc();
821 $plist = "$table_cur->table_token|$user_cur->table|$table_cur->player_n";
822 for ($i = 0 ; $i < $table_cur->player_n ; $i++) {
823 $plist .= '|'.$this->user[$table_cur->player[$i]]->sess;
825 log_legal($curtime, 'xxx', $user_cur, "STAT:DESTROY_GAME(RECOVERY)", $plist);
827 $this->room_join_wakeup($user_cur, TRUE, -2);
828 $table_cur->table_token = "";
831 } // if ($table_cur->player_n == PLAYERS_N) {
832 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
834 log_rd2("out new loop.");
836 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
837 $user_cur = $this->user[$i];
839 log_rd2("User: ".$user_cur->name." stat: ".$user_cur->stat." subst: ".$user_cur->subst);
841 if ($user_cur->sess == "")
844 if ($user_cur->lacc + EXPIRE_TIME_RD < $curtime) {
845 // Auto logout dell'utente
846 log_rd2("AUTO LOGOUT.".($user_cur->lacc + EXPIRE_TIME_RD)." curtime ".$curtime);
848 if ($user_cur->stat == 'table' || $user_cur->stat == 'room') {
849 log_auth($user_cur->sess, "Autologout session.");
853 log_rd2("AUTO LOGOUT.");
854 if ($user_cur->subst == 'sitdown' || $user_cur->stat == 'table')
855 $this->room_wakeup($user_cur);
856 else if ($user_cur->subst == 'standup')
857 $this->room_outstandup($user_cur);
859 log_rd2("LOGOUT FROM WHAT ???");
863 if ($user_cur->laccwr + EXPIRE_TIME_SMAMMA < $curtime) { // lo rimettiamo in piedi
864 if ($user_cur->stat == 'room' && $user_cur->subst == 'sitdown') {
865 $this->room_wakeup($user_cur);
866 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
867 /* 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" */
868 $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);
869 $user_cur->step_inc();
873 log_rd2("GARBAGE UPDATED!");
875 // externalized $this->garbage_timeout = time() + GARBAGE_TIMEOUT;
876 Room::garbage_time_expire_set($curtime + GARBAGE_TIMEOUT);
882 function show_room($user_step, $user)
884 GLOBAL $G_lang, $mlang_brisk;
885 log_main("show_room: username: ".$user->name);
887 $ret = sprintf('gst.st = %d; ', $user_step);
889 if ($user->flags & USER_FLAG_ISOLAUTH) {
890 $ret .= 'list_set(\'isolation\', false, \''.$mlang_brisk['tit_onisol'][$G_lang].'\' ); ';
892 else if ($user->flags & USER_FLAG_LISTAUTH) {
893 $ret .= 'list_set(\'auth\', false, \''.$mlang_brisk['tit_onauth'][$G_lang].'\' ); ';
896 $ret .= 'list_set(\'all\', false, \'\' ); ';
899 if ($user->subst == 'standup')
900 $ret .= "tra.show(); ";
902 $ret .= "tra.hide(); ";
904 $ret .= sprintf('stat = "%s";', $user->stat);
906 $ret .= root_wellcome($user);
907 if ($user->flags & USER_FLAG_DBFAILED) {
908 $ret .= "gst.st = ".($user->step+1)."; ";
909 $ret .= show_notify($mlang_brisk['db_failed'][$G_lang], 0, $mlang_brisk['btn_close'][$G_lang], 400, 140);
912 $ret .= sprintf('subst = "%s";', $user->subst);
913 $ret .= $user->myname_innerHTML();
915 for ($i = 0 ; $i < TABLES_N ; $i++) {
917 $ret .= $this->table_content($user, $i);
918 // $ret .= table_act_content(($user->subst == 'standup'), $this->table[$i]->player_n, $i, $user->table,
919 // ($this->table[$i]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
920 $ret .= $this->table[$i]->act_content($user);
921 if ($this->table[$i]->wag_own != -1)
922 $ret .= sprintf('tra.add(%d, "%s: %s"); ', $i, $this->user[$this->table[$i]->wag_own]->name, $this->table[$i]->wag_com);
924 $ret .= sprintf('tra.rem(%d); ', $i);
926 $ret .= $this->standup_content($user);
932 function room_wakeup($user)
934 $table_idx = $user->table;
935 $table = $this->table[$table_idx];
937 log_main("WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
941 $from_table = ($user->stat == "table");
943 log_main("WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
945 for ($i = 0 ; $i < $table->player_n ; $i++) {
946 $user_cur = $this->user[$table->player[$i]];
947 log_main("PREIMPOST: INLOOP name: ".$user_cur->name);
949 if ($user->idx_get() != $table->player[$i]) {
950 $user_cur->stat_set("room");
951 $user_cur->subst = "sitdown";
952 $user_cur->laccwr = $curtime;
954 else if ($user->sess != "") {
955 $user_cur->stat_set("room");
956 $user_cur->subst = "standup";
957 $user_cur->laccwr = $curtime;
958 $user_cur->table = -1;
963 $user->stat_set("room");
964 $user->subst = "standup";
965 $user->laccwr = $curtime;
968 $remove_wagon = FALSE;
969 if($table->wag_own == $user->idx_get()) {
970 $table->wag_reset($curtime);
971 $remove_wagon = TRUE;
975 /* aggiorna l'array dei giocatori al tavolo. */
976 $table->user_rem($this, $user);
978 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
979 $user_cur = $this->user[$i];
980 if ($user_cur->sess == '' || $user_cur->stat != 'room')
983 // log_main("VALORI: name: ".$user_cur->name."from_table: ".$from_table." tab: ".$user_cur->table." taix: ".$table_idx." ucur: ".$user_cur." us: ".$user);
985 $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
986 if ($from_table && ($user_cur->table == $table_idx || $user->idx_get() == $i)) {
987 $ret .= 'gst.st_loc++; hstm.stop(); window.onunload = null; window.onbeforeunload = null; document.location.assign("index.php");|';
988 // $ret .= 'gst.st_loc++; document.location.assign("index.php");|';
989 log_main("DOCUMENT.index.php: from table");
991 else if ($user_cur->stat == "room") {
992 log_main("DOCUMENT.index.php: from table");
994 $ret .= $this->table_content($user_cur, $table_idx);
995 $ret .= $this->standup_content($user_cur);
997 // $ret .= table_act_content(FALSE, 0, $table_idx, $user->table, FALSE);
998 $ret .= $table->act_content($user);
1000 if ($user->idx_get() == $i) {
1001 // set the new status
1002 $ret .= 'subst = "standup"; tra.show(); ';
1003 // clean the action buttons in other tables
1004 for ($e = 0 ; $e < TABLES_N ; $e++) {
1005 if ($this->table[$e]->player_n < PLAYERS_N) {
1006 // $ret .= table_act_content(TRUE, 0, $e, $user->table,
1007 // ($this->table[$e]->auth_only == FALSE ? TRUE : $user->flags & USER_FLAG_AUTH));
1008 $ret .= $this->table[$e]->act_content($user);
1013 // $ret .= table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table,
1014 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1015 $ret .= $table->act_content($user_cur);
1018 log_wr("ROOM_WAKEUP: ".$ret);
1019 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1020 $user_cur->step_inc();
1024 function room_join_wakeup($user, $update_lacc = FALSE, $trans_delta)
1026 $table_idx = $user->table;
1027 $table = $this->table[$table_idx];
1029 log_main("JOIN_WAKEUP: begin function table:".$table_idx." stat: ".$user->stat." subst: ".$user->subst);
1032 $user_wup = array();
1034 $user_tab = array();
1036 log_main("JOIN WAKEUP: from table [".$user->table."] nplayers_n: ".$this->table[$user->table]->player_n);
1038 for ($i = 0 ; $i < $table->player_n ; $i++) {
1039 $user_cur = $this->user[$table->player[$i]];
1040 log_main("PREIMPOST INLOOP name: ".$user_cur->name);
1041 if ($user_cur->sess != "") {
1042 if ($update_lacc == TRUE) {
1043 $user_cur->laccwr = $curtime;
1045 log_main("cur: ".$user_cur->name." subst: ".$user_cur->subst);
1046 if ($user_cur->subst == "shutdowned") {
1047 $user_cur->stat_set("room");
1048 $user_cur->subst = "sitdown";
1050 else if ($user_cur->subst == "shutdowner") {
1051 $user_cur->stat_set("room");
1052 $user_cur->subst = "standup";
1053 $user_cur->table = -1;
1054 $user_wup[$user_wup_n++] = $user_cur;
1056 $remove_wagon = FALSE;
1057 if($table->wag_own == $table->player[$i]) {
1058 $remove_wagon = TRUE;
1059 $table->wag_reset($curtime);
1062 $user_tab[$user_tab_n++] = $table->player[$i];
1066 for ($wup_idx = 0 ; $wup_idx < $user_wup_n ; $wup_idx++)
1067 $table->user_rem($this, $user_wup[$wup_idx]);
1069 /* aggiorna l'array dei giocatori al tavolo. */
1071 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1072 log_main("START LOOP");
1073 $user_cur = $this->user[$i];
1074 if ($user_cur->sess == '' || $user_cur->stat != 'room') {
1075 log_main("name: ".$user_cur->name."skip subst: ".$user_cur->subst);
1080 log_main("VALORI name: ".$user_cur->name." tab: ".$user_cur->table." taix: ".$table_idx);
1082 $ret = "gst.st = ".($user_cur->step+1)."; ".($remove_wagon ? sprintf("tra.rem(%d);",$table_idx) : "");
1083 if ($user_cur->stat == "room") {
1084 log_main("DOCUMENT.index.php from table");
1086 $ret .= $this->table_content($user_cur, $table_idx);
1087 $ret .= $this->standup_content($user_cur);
1089 // $ret .= table_act_content(FALSE, 0, $table_idx, $user_cur->table,
1090 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1091 $ret .= $table->act_content($user_cur);
1094 for ($tab_idx = 0 ; $tab_idx < $user_tab_n ; $tab_idx++)
1095 if ($user_tab[$tab_idx] == $i)
1098 // for users that wakeup the room will be reconstructed by index_rd.php
1099 if ($tab_idx < $user_tab_n) {
1100 log_main("PRE show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1102 // ARRAY_POP DISABLED
1103 // if ($trans_delta == 0)
1104 // while (array_pop($user_cur->comm) != NULL);
1106 $user_cur->trans_step = $user_cur->step + 1 + $trans_delta;
1107 $user_cur->comm[$user_cur->step % COMM_N] = "";
1108 $user_cur->step_inc();
1109 $user_cur->comm[$user_cur->step % COMM_N] = $this->show_room(($user_cur->step + 1), $user_cur);
1110 $user_cur->step_inc();
1111 log_main("POST show_room username: ".$user_cur->name." STEP: ".$user_cur->step);
1115 log_main("JOIN_WAKEUP wup_idx ".$wup_idx." wup_n ".$user_wup_n);
1117 log_main("JOIN_WAKEUP more");
1118 // $ret .= table_act_content(($user_cur->subst == 'standup'), $table->player_n, $table_idx, $user_cur->table,
1119 // ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1120 $ret .= $table->act_content($user_cur);
1122 log_main("JOIN_WAKEUP end more");
1124 log_wr("ROOM_JOIN_WAKEUP: ".$ret);
1125 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1126 $user_cur->step_inc();
1130 function room_outstandup($user)
1132 $this->room_sitdown($user, -1);
1135 function table_update($user)
1137 log_main("table_update: pre - USER: ".$user->name);
1139 $table_idx = $user->table;
1141 if ($table_idx > -1)
1142 $table = $this->table[$table_idx];
1144 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1146 $user_cur = $this->user[$i];
1147 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1150 $ret = "gst.st = ".($user_cur->step+1)."; ";
1151 if ($table_idx > -1)
1152 $ret .= $this->table_content($user_cur, $table_idx);
1154 if ($user->idx_get() == $i) {
1155 $ret .= $user->myname_innerHTML();
1157 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1158 $user_cur->step_inc();
1161 log_main("table_update: post");
1164 function room_sitdown($user, $table_idx)
1166 log_main("room_sitdown ".($user == FALSE ? "USER: FALSE" : "USER: ".$user->name));
1170 if ($table_idx > -1 && $table_idx < TABLES_N) {
1171 $table = $this->table[$table_idx];
1174 if ($table->wag_own != -1 && $table->player_n == PLAYERS_N) {
1175 for ($i = 0 ; $i < TABLES_N ; $i++) {
1176 if ($table->wag_own == $table->player[$i]) {
1177 $train_app = sprintf("tra.rem(%d); ", $table_idx);
1178 $table->wag_reset(time());
1185 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1187 $user_cur = $this->user[$i];
1188 if ($user_cur->sess == '' || $user_cur->stat != 'room')
1191 $ret = "gst.st = ".($user_cur->step+1)."; ".$train_app;
1192 if ($table_idx > -1)
1193 $ret .= $this->table_content($user_cur, $table_idx);
1194 $ret .= $this->standup_content($user_cur);
1196 if ($user->idx_get() == $i) {
1197 $ret .= 'subst = "sitdown"; tra.hide(); ';
1198 // clean the action buttons in other tables
1199 for ($e = 0 ; $e < TABLES_N ; $e++) {
1200 // $ret .= table_act_content(FALSE, 0, $e, $user_cur->table, FALSE);
1201 $ret .= $this->table[$e]->act_content($user_cur);
1204 else if ($table_idx > -1) {
1205 if ($table->player_n == PLAYERS_N) {
1206 // $ret .= table_act_content(($user_cur->subst == 'standup'), PLAYERS_N, $table_idx, $user_cur->table,
1207 /// ($table->auth_only == FALSE ? TRUE : $user_cur->flags & USER_FLAG_AUTH));
1208 $ret .= $table->act_content($user_cur);
1211 $user_cur->comm[$user_cur->step % COMM_N] = $ret;
1212 $user_cur->step_inc();
1216 function chatt_send($user, $mesg)
1218 GLOBAL $G_base, $G_alarm_passwd, $mlang_brisk, $G_lang;
1222 $msg = substr($mesg, 6, 128);
1224 $dt = date("H:i ", $curtime);
1235 $is_normchat = FALSE;
1236 /* for old isolation management $is_ticker = FALSE; */
1237 $update_room = FALSE;
1239 if (strcmp($msg, "/tav") == 0 ||
1240 strncmp($msg, "/tav ", 5) == 0) {
1242 if ($user->stat != 'room' || $user->subst != 'sitdown') {
1243 /* 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>" */
1244 $msg = $mlang_brisk['tickmust'][$G_lang];
1245 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1250 $table = $this->table[$user->table];
1252 if ($table->wag_own != -1) {
1253 // MLANG <br>Il messaggio di segnalazione del tavolo è già attivato.<br><br>
1254 $msg = $mlang_brisk['tickjust'][$G_lang];
1255 $to_user = show_notify($msg, 0, "chiudi", 400, 100);
1260 $dtime = $curtime - $table->wag_tout;
1261 if ($dtime < EXPIRE_TIME_WAG) {
1262 // MLANG - <br>Il messaggio di segnalazione del tavolo<br>è disattivato ancora per %d second%s.<br><br>
1263 $msg = sprintf($mlang_brisk['tickwait'][$G_lang],
1264 EXPIRE_TIME_WAG - $dtime, (EXPIRE_TIME_WAG - $dtime == 1 ? ($G_lang == 'en' ? "" : "o") : ($G_lang == 'en' ? "s" : "i")));
1265 $to_user = show_notify($msg, 0, $mlang_brisk['btn_close'][$G_lang] , 400, 100);
1270 $msg = substr($msg, 5);
1272 $table->wag_set($user->idx_get(), $msg);
1273 $to_user = sprintf('tra.add(%d, "%s");', $user->table, xcape(sprintf("%s: %s", $user->name, $msg)));
1274 $to_room = $to_user;
1275 /* for old isolation management $is_ticker = TRUE; */
1277 } // /tav chat command
1279 else if (strncmp($msg, "/alarm ", 7) == 0) {
1280 if (strncmp($msg, "/alarm to ", 10) == 0) {
1281 $sp_pos = strpos($msg, " ", 10);
1282 $target = substr($msg, 10, $sp_pos - 10);
1283 $alarm_check = "/alarm to ".$target." ".$G_alarm_passwd." ";
1287 $alarm_check = "/alarm ".$G_alarm_passwd." ";
1290 if (strncmp($msg, $alarm_check, strlen($alarm_check)) != 0) {
1291 /* MLANG: "<br>La password digitata non è corretta.<br><br>" */
1292 $msg = $mlang_brisk['alarpass'][$G_lang];
1293 $to_user = show_notify($msg, 0, $mlang_brisk['btn_close'][$G_lang], 400, 100);
1298 /* MLANG: "Alarm <b>%s</b> inviato a <b>%s</b>." */
1299 $prestr = sprintf($mlang_brisk['alarret'][$G_lang], xcape(substr($msg, strlen($alarm_check))),
1300 ($target == "" ? $mlang_brisk['tit_all'][$G_lang] : xcape($target)) );
1301 $to_user = sprintf('chatt_sub("%s", [2, "%s"],%s);',
1302 $dt, NICKSERV, $prestr);
1304 $msg = sprintf("<br><b>%s<br><br>%s</b><br><br>",
1305 $dt.NICKSERV, xcape(substr($msg, strlen($alarm_check))));
1306 /* MLANG: "chiudi" */
1307 $to_all = show_notify($msg, 0, $mlang_brisk['btn_close'][$G_lang], 400, 120);
1309 } // /alarm chat command
1310 else if (strncmp($msg, "/listen ", 8) == 0) {
1311 $arg = substr($msg, 8);
1313 if (strcasecmp($arg, "isolation") == 0) {
1315 if ($user->stat == 'room' && $user->subst == 'sitdown' &&
1316 $user->table >= TABLES_AUTH_N) {
1317 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['listmust'][$G_lang]);
1321 $user->flags &= ~USER_FLAG_MAP_AUTH;
1322 $user->flags |= USER_FLAG_ISOLAUTH;
1323 $to_user = 'list_set(\'isolation\', true, \''.$mlang_brisk['tit_onisol'][$G_lang].'\'); ';
1326 else if (strcasecmp($arg, "auth") == 0) {
1327 $flags_old = $user->flags;
1328 $user->flags &= ~USER_FLAG_MAP_AUTH;
1329 $user->flags |= USER_FLAG_LISTAUTH;
1330 $to_user = 'list_set(\'auth\', true, \''.$mlang_brisk['tit_onauth'][$G_lang].'\'); ';
1333 $flags_old = $user->flags;
1334 $user->flags &= ~USER_FLAG_MAP_AUTH;
1335 $to_user = 'list_set(\'all\', true, \'\'); ';
1338 // if from isolation redraw standup area
1339 if (($flags_old ^ $user->flags) & USER_FLAG_ISOLAUTH) {
1340 $to_user .= 'standup_data_old = null; '.$this->standup_content($user);
1344 else if (strcmp($msg, "/authreq") == 0) {
1345 if ($user->flags & USER_FLAG_AUTH) {
1346 $to_user = sprintf('authbox(300,200);');
1349 /* 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." */
1350 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['authmust'][$G_lang]);
1353 else if (strncmp($msg, "/mesgtoadm", 8) == 0) {
1354 if ($user->flags & USER_FLAG_AUTH) {
1355 $to_user = sprintf('mesgtoadmbox(500,300);');
1358 /* MLANG: "<b>Per inviare un messaggio devi essere autenticato.</b>" */
1359 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['mesgmust'][$G_lang]);
1362 else if (strncmp($msg, "/nick ", 6) == 0) {
1363 log_main("chatt_send BEGIN");
1366 if (($name_new = validate_name(substr($msg, 6))) == FALSE) {
1367 $to_user = sprintf('chatt_sub("%s", [2,"%s"],"%s");', $dt, NICKSERV, $mlang_brisk['nickmust'][$G_lang]);
1371 $msg = "COMMAND ".$msg;
1372 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1373 $user_cur = $this->user[$i];
1375 if ($user_cur->sess == '')
1377 if (strcasecmp($user_cur->name,$name_new) == 0)
1380 if ($i < MAX_PLAYERS) {
1381 $prestr = sprintf($mlang_brisk['nickdupl'][$G_lang], xcape($name_new));
1382 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $prestr);
1386 /* 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>" */
1387 if ($user->flags & USER_FLAG_AUTH) {
1388 if (strcasecmp($user->name,$name_new) != 0) {
1389 if (( ($user->flags & USER_FLAG_MAP_AUTH) != USER_FLAG_ISOLAUTH) &&
1390 ($user->subst == 'standup' ||
1391 ($user->subst != 'standup' && $this->table[$user->table]->auth_only == FALSE)
1394 $user->flags &= ~(USER_FLAG_AUTH | USER_FLAG_TY_ALL); // Remove auth if name changed
1395 for ($i = 0 ; $i < TABLES_N ; $i++) {
1396 $to_user .= $this->table[$i]->act_content($user);
1400 $to_user = sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $mlang_brisk['authchan'][$G_lang]);
1405 $user->name = $name_new; // OK - nick changed
1406 /* se nome gia' in uso, segnala cosa potrebbe capitare */
1407 if (($user->flags & USER_FLAG_AUTH) == 0) {
1408 if (($bdb = BriskDB::create()) != FALSE) {
1410 /* 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>" */
1411 if ($bdb->login_exists($name_new)) {
1412 $prestr = sprintf($mlang_brisk['nickjust'][$G_lang], xcape($name_new));
1413 $to_user .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, $prestr);
1418 log_main("chatt_send start set");
1420 $update_room = TRUE;
1422 } // nick chat command
1424 else if (strncmp($msg, "/st ", 4) == 0) {
1425 log_main("chatt_send BEGIN");
1428 $st_str = substr($msg, 4);
1430 if (strcasecmp($st_str, "normale") == 0) {
1431 $st = USER_FLAG_S_NORM;
1433 else if (strcasecmp($st_str, "pausa") == 0) {
1434 $st = USER_FLAG_S_PAU;
1436 else if (strcasecmp($st_str, "fuori") == 0) {
1437 $st = USER_FLAG_S_OUT;
1439 else if (strcasecmp($st_str, "cane") == 0) {
1440 $st = USER_FLAG_S_DOG;
1442 else if (strcasecmp($st_str, "cibo") == 0) {
1443 $st = USER_FLAG_S_EAT;
1445 else if (strcasecmp($st_str, "lavoro") == 0) {
1446 $st = USER_FLAG_S_WRK;
1448 else if (strcasecmp($st_str, "sigaretta") == 0) {
1449 $st = USER_FLAG_S_SMK;
1451 else if (strcasecmp($st_str, "presente") == 0) {
1452 $st = USER_FLAG_S_EYE;
1454 else if (strcasecmp($st_str, "coniglio") == 0) {
1455 $st = USER_FLAG_S_RABB;
1457 else if (strcasecmp($st_str, "calcio") == 0) {
1458 $st = USER_FLAG_S_SOCC;
1460 else if (strcasecmp($st_str, "pupo") == 0) {
1461 $st = USER_FLAG_S_BABY;
1463 else if (strcasecmp($st_str, "pulizie") == 0) {
1464 $st = USER_FLAG_S_MOP;
1467 /* MLANG: "Questo stato non esiste." */
1468 $to_user = sprintf('chatt_sub("%s", [2,"%s"],"%s");', $dt, NICKSERV, $mlang_brisk['statunkn'][$G_lang]);
1472 log_main("chatt_send start set");
1473 if (($user->flags & USER_FLAG_S_ALL) != $st) {
1474 $update_room = TRUE;
1475 $user->flags = ($user->flags & ~USER_FLAG_S_ALL) | $st;
1478 } // nick chat command
1480 else { // normal chat line
1481 $is_normchat = TRUE;
1482 if (CHAT_ENABLED && $curtime < ($user->chat_ban + $user->chat_dlt)) {
1484 $user->chat_dlt = $user->chat_dlt * 2;
1485 if ($user->chat_dlt > 120)
1486 $user->chat_dlt = 120;
1488 else if ($user->chat_lst == $msg)
1490 else if (CHAT_ENABLED && $curtime - $user->chattime[($user->chat_cur + 1) % CHAT_N] < CHAT_ILL_TIME) {
1491 $user->chat_ban = $curtime;
1492 $user->chat_dlt = 5;
1496 $user->chat_ban = 0;
1497 $user->chat_dlt = 0;
1501 $to_user = sprintf('chatt_sub("%s", [%d, "%s"],"%s");', $dt, $user->flags, xcape($user->name), xcape("== chat ban =="));
1504 $to_user = sprintf('chatt_sub("%s", [%d, "%s"],"%s");', $dt, $user->flags, xcape($user->name), xcape($msg));
1505 // temporary silentiation for troll (will became array check)
1506 // if (strcasecmp($user->name,'JackRokka') != 0 && $user->sess != '47ea653f602e8')
1507 $to_room = $to_user;
1510 log_legal($curtime, 'xxx', $user,
1511 ($user->stat == 'room' ? 'room' : 'table '.$user->table),$msg);
1513 $user->chat_lst = "$msg";
1514 $user->chattime[$user->chat_cur % CHAT_N] = $curtime;
1524 // Output to clients
1527 if ($to_user != FALSE) {
1528 $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
1529 $user->comm[$user->step % COMM_N] .= $to_user;
1533 if ($to_room != FALSE) {
1534 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1535 $user_cur = $this->user[$i];
1536 if ($target != "" && $user_cur->name != $target)
1538 if ($user_cur->sess == '' || $user_cur->stat == 'table' || $user->idx_get() == $i)
1541 if ($is_normchat == TRUE) {
1542 // use MAP_AUTH to check if auth or isolation
1543 if ($user_cur->flags & USER_FLAG_MAP_AUTH) {
1544 if (($user->flags & USER_FLAG_AUTH) == 0) {
1550 else if ($is_ticker) {
1551 if (($user_cur->flags & USER_FLAG_MAP_AUTH) == USER_FLAG_ISOLAUTH) {
1552 if ($user->table >= TABLES_AUTH_N)
1557 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ";
1558 $user_cur->comm[$user_cur->step % COMM_N] .= $to_room;
1559 $user_cur->step_inc();
1564 // FIXME BRISK4: include for each kind of table
1565 require_once("${G_base}briskin5/Obj/briskin5.phh");
1566 // Before all align times with table timeout
1567 for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1568 $table_cur = $this->table[$table_idx];
1569 // if the table is complete and exists its shared mem we get the info about users lacc
1571 if ($table_cur->player_n == PLAYERS_N) {
1572 log_main("PLAYERS == N TABLE ".$table_idx);
1574 if (($sem = Bin5::lock_data(TRUE, $table_idx)) != FALSE) {
1575 log_main("bin5 lock data success");
1577 $no_recovery = FALSE;
1578 if (($bri = Bin5::load_data($table_idx)) != FALSE) {
1579 if ($table_cur->table_token != $bri->table_token) {
1580 log_main("ERROR: not matching table_token. Room: ".$table_cur->table_token." Table: ".$bri->table_token);
1585 if ($bri != FALSE) {
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 Bin5::save_data($bri);
1600 Bin5::unlock_data($sem);
1602 } // if ($table_cur->player_n == PLAYERS_N) {
1603 } // for ($table_idx = 0 ; $table_idx < TABLES_N ; $table_idx++) {
1604 } // if ($to_tabl == true ...
1607 if ($user->stat == 'room' && $user->subst == 'standup') {
1608 $this->standup_update($user);
1610 else if ($user->stat == 'room' && $user->subst == 'sitdown') {
1611 log_main("chatt_send pre table update");
1612 $this->table_update($user);
1613 log_main("chatt_send post table update");
1615 } // if ($update_room ...
1618 } // function chatt_send( ...
1620 function get_user($sess, &$idx)
1624 if (validate_sess($sess)) {
1625 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1626 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1629 $ret = $this->user[$i];
1633 log_main(sprintf("get_user: Wrong sess from page [%s]",$PHP_SELF));
1634 // for ($i = 0 ; $i < MAX_PLAYERS ; $i++)
1635 // log_main(sprintf("get_user: Wrong sess compared with [%s]",$this->user[$i]->sess));
1638 log_main(sprintf("get_user: Wrong strlen [%s]",$sess));
1645 * function add_user(&$room, &$sess, &$idx, $name, $pass, $ip)
1648 * if ($idx > -1 && ret == FALSE) => duplicated nick
1649 * if ($idx == -2 && ret == FALSE) => invalid name
1650 * if ($idx == -3 && ret == FALSE) => wrong password
1651 * if ($idx == -1 && ret == FALSE) => no space left
1652 * if ($idx == 0 && ret == user) => SUCCESS
1653 * if ($idx == -$idx && ret == user) => SUCCESS (but the login exists in the auth db)
1656 function add_user(&$sess, &$idx, $name, $pass, $ip)
1658 GLOBAL $G_base, $CO_list;
1662 $authenticate = FALSE;
1664 $login_exists = FALSE;
1666 $ghost_auth = FALSE;
1671 if (($name_new = validate_name($name)) == FALSE) {
1676 log_auth("XXX", sprintf("ARRIVA: [%s] pass:[%s]", $sess, ($pass == FALSE ? "FALSE" : $pass)));
1677 if (validate_sess($sess) == FALSE)
1680 /* if pass != FALSE verify the login with pass */
1681 log_auth("XXX", "auth1");
1683 if (($bdb = BriskDB::create()) != FALSE) {
1685 if ($pass != FALSE) { // TODO: here add a method to $bdb to check if the db is available.
1686 log_auth("XXX", "auth2");
1687 $authenticate = $bdb->login_verify($name_new, $pass, $code);
1688 log_auth("XXX", "authenticate: ".($authenticate != FALSE ? "TRUE" : "FALSE"));
1690 if ($authenticate != FALSE) {
1691 $user_type = $authenticate->type_get();
1699 $login_exists = $bdb->login_exists($name_new);
1703 // if db is down, send a warning and verify only current users
1704 // no actions at this moment
1706 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1708 if (strcmp($sess, $this->user[$i]->sess) == 0) {
1712 if ($idfree == -1 && strcmp($this->user[$i]->sess, "") == 0) {
1714 continue; // NOTE: CHECK IT !!
1716 if (strcasecmp($this->user[$i]->name, $name_new) == 0) {
1717 if ($authenticate != FALSE) {
1719 $ghost_auth = ($this->user[$i]->flags & USER_FLAG_AUTH);
1730 log_auth("XXX", sprintf("TROVATO A QUESTO PUNTO [%d] sess [%s] name [%s]", $idx, $sess, $name_new));
1732 /* there is another user logged with your account and you and him have authenticated => new user
1733 get the session of the old user */
1734 if ($ghost > -1 && $ghost_auth && ($authenticate != FALSE)) {
1737 $ghost_user = $this->user[$ghost];
1739 $ghost_user->step_inc();
1742 $ghost_user->sess = $sess;
1745 $ghost_user->sess = $sess;
1748 // If user at the table we need to update the table data too
1749 $table_idx = $ghost_user->table;
1750 if ($ghost_user->stat == "table" && $this->table[$table_idx]->player_n == PLAYERS_N) {
1751 // FIXME BRISK4: include for each kind of table
1752 require_once("${G_base}briskin5/Obj/briskin5.phh");
1753 if (($brisem = Bin5::lock_data(TRUE, $table_idx)) != FALSE) {
1754 if (($bri = Bin5::load_data($table_idx)) != FALSE) {
1755 if ($bri->the_end != TRUE) {
1756 $bri->user[$ghost_user->table_pos]->step_inc();
1757 $bri->user[$ghost_user->table_pos]->sess = $sess;
1758 Bin5::save_data($bri);
1761 Bin5::unlock_data($brisem);
1766 return ($this->user[$ghost]);
1768 else if ($idx != -1 && $i == MAX_PLAYERS) {
1773 $this->user[$idx]->sess = $sess;
1776 $this->user[$idx]->sess = $sess;
1778 $this->user[$idx]->name = $name_new; // OK - add new user
1779 $this->user[$idx]->stat_set("room");
1780 $this->user[$idx]->step_set(0);
1781 while (array_pop($this->user[$idx]->comm) != NULL);
1782 $this->user[$idx]->subst = "standup";
1783 $this->user[$idx]->lacc = $curtime;
1784 $this->user[$idx]->laccwr = $curtime;
1785 $this->user[$idx]->bantime = 0;
1786 $this->user[$idx]->ip = $ip;
1788 $this->user[$idx]->flags = $user_type;
1789 $this->user[$idx]->flags |= ($authenticate != FALSE ? USER_FLAG_AUTH : 0x00);
1790 $this->user[$idx]->flags |= ( ($pass != FALSE && $bdb == FALSE) ? USER_FLAG_DBFAILED : 0x00);
1791 log_auth("XXX", sprintf("FLAGS: [%x]", $this->user[$idx]->flags));
1794 if ($authenticate != FALSE) {
1795 $this->user[$idx]->code = $authenticate->code_get();
1796 $this->user[$idx]->flags |= USER_FLAG_LISTAUTH;
1798 if (isset($CO_list)) {
1799 if (strcmp($CO_list, "auth") == 0) {
1800 $this->user[$idx]->flags &= ~USER_FLAG_MAP_AUTH;
1801 $this->user[$idx]->flags |= USER_FLAG_LISTAUTH;
1803 if (strcmp($CO_list, "isolation") == 0) {
1804 $this->user[$idx]->flags &= ~USER_FLAG_MAP_AUTH;
1805 $this->user[$idx]->flags |= USER_FLAG_ISOLAUTH;
1808 $this->user[$idx]->flags &= ~USER_FLAG_MAP_AUTH;
1814 log_main("ghost: rename!");
1815 $ghost_user = $this->user[$ghost];
1817 if ($ghost_auth == FALSE) {
1818 for ($sfx = 1 ; $sfx <= MAX_PLAYERS ; $sfx++) {
1819 $ghostname = 'ghost'.$sfx;
1820 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1821 if (strcmp("", $this->user[$i]->sess) == 0)
1824 if (strcasecmp($this->user[$i]->name, $ghostname) == 0) {
1829 if ($ghostname != '')
1833 $ghost_user->name = $ghostname;
1835 if ($ghost_user->stat == 'room' && $ghost_user->subst == 'standup') {
1836 $this->standup_update($ghost_user);
1839 log_main("chatt_send pre table update");
1840 $this->table_update($ghost_user);
1841 log_main("chatt_send post table update");
1843 } // if ($ghost_auth == FALSE
1845 // FIXME: cacciare il vecchio utente room && table (if needed)
1846 $ghost_user->the_end = TRUE;
1847 $ghost_user->lacc = 0;
1848 $this->garbage_manager(TRUE);
1850 } // if ($ghost > -1) {
1855 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));
1857 $ret = $this->user[$real_idx];
1864 function standup_update($user)
1866 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1867 $user_cur = $this->user[$i];
1868 if ($user_cur->sess == '')
1871 log_main("STANDUP START: ".$user_cur->stat);
1873 if ($user_cur->stat == 'room') {
1874 $user_cur->comm[$user_cur->step % COMM_N] = "gst.st = ".($user_cur->step+1)."; ".$this->standup_content($user_cur);
1875 if ($user->idx_get() == $i) {
1876 $user_cur->comm[$user_cur->step % COMM_N] .= $user->myname_innerHTML();
1878 log_main("FROM STANDUP: NAME: ".$user_cur->name." SENDED: ".$user_cur->comm[$user_cur->step % COMM_N]);
1880 $user_cur->step_inc();
1886 static function create()
1894 function load_data()
1899 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1) {
1900 log_main("ftok failed");
1904 if (($shm_sz = sharedmem_sz($tok)) == -1) {
1905 log_main("shmop_open failed");
1909 $shm_sz = SHM_DIMS_MIN;
1911 if ($shm = shm_attach($tok, $shm_sz)) {
1912 $room = @shm_get_var($shm, $tok); // CHECKED BELOW
1914 log_only("bri == ".($room == FALSE ? "FALSE" : "TRUE")." bri === ".($room === FALSE ? "FALSE" : "TRUE")." bri isset ".(isset($room) ? "TRUE" : "FALSE"));
1916 log_only("bri count ".count($room));
1918 if ($room == FALSE) {
1919 log_only("INIT MAIN DATA");
1922 $room = Room::create();
1924 log_shme("Room::create");
1926 if (Room::save_data($room) == FALSE)
1931 $room->shm_sz = $shm_sz;
1935 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
1936 if (($room->user[$i] = User::load_data($i, FALSE)) == FALSE) {
1937 log_crit("User::load_data failed");
1941 if ($i < MAX_PLAYERS) {
1947 // SHSPLIT: load users from the shared memory
1956 function save_data_orig($room)
1964 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1)
1967 while ($room->shm_sz < SHM_DIMS_MAX) {
1968 if (($shm = shm_attach($tok, $room->shm_sz)) == FALSE)
1971 // log_only("PUT_VAR DI ".strlen(serialize($room)));
1972 if (@shm_put_var($shm, $tok, $room) != FALSE) {
1976 if (shm_remove($shm) === FALSE) {
1977 log_only("REMOVE FALLITA");
1981 $room->shm_sz += SHM_DIMS_DLT;
1991 function save_data($room)
1998 if (($tok = @ftok(FTOK_PATH."/main", "B")) == -1)
2001 // SHSPLIT: before save the $room you must save users,
2002 // detach from main struct and (then) reattach
2003 $user_park = array();
2004 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
2005 $user_park[$i] = $room->user[$i];
2006 $room->user[$i] = FALSE;
2009 while ($room->shm_sz < SHM_DIMS_MAX) {
2010 if (($shm = shm_attach($tok, $room->shm_sz)) == FALSE)
2013 // log_only("PUT_VAR DI ".strlen(serialize($room)));
2014 if (@shm_put_var($shm, $tok, $room) != FALSE) {
2015 log_shme("Room::save_data");
2019 if (shm_remove($shm) === FALSE) {
2020 log_only("REMOVE FALLITA");
2024 $room->shm_sz += SHM_DIMS_DLT;
2030 // SHSPLIT: reattach users to the room class
2031 for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
2032 User::save_data($user_park[$i], $i);
2033 $room->user[$i] = $user_park[$i];
2035 log_load("FINISH: ".($ret == TRUE ? "TRUE" : "FALSE"));
2040 static function lock_data($is_exclusive)
2042 if (($res = file_lock(FTOK_PATH."/main", $is_exclusive)) != FALSE) {
2043 self::$delta_t = microtime(TRUE);
2044 log_lock("LOCK room [".self::$delta_t."]");
2052 static function unlock_data($res)
2056 log_lock("UNLOCK room [".(microtime(TRUE) - (self::$delta_t))."]");
2062 function standup_content($user)
2067 if ($user->stat != 'room')
2070 for ($i = 0 , $ct = 0 ; $ct < 4 && $i < MAX_PLAYERS ; $i++) {
2071 if ($this->user[$i]->sess == "" || $this->user[$i]->stat != "room" || $this->user[$i]->name == "")
2076 // $content .= sprintf('<table cols=\\"%d\\" class=\\"table_standup\\">', $ct);
2078 $content = ' j_stand_cont( [ ';
2080 for ($i = 0 , $ct = 0 ; $i < MAX_PLAYERS ; $i++) {
2081 if ($this->user[$i]->sess == "" || $this->user[$i]->stat != "room" || $this->user[$i]->name == "")
2084 $flags = $this->user[$i]->flags;
2086 if ($this->user[$i]->subst == "standup") {
2087 if ($user->idx_get() == $i) {
2091 $content .= sprintf('%s[ %d, "%s" ]',($ct > 0 ? ', ' : ''), $flags, xcape($this->user[$i]->name));
2100 function table_content($user, $table_idx)
2106 // Si possono usare i dati nella classe table
2109 $sess = $user->sess;
2110 $table = $this->table[$table_idx];
2112 if ($user->stat != 'room')
2116 for ($i = 0 ; $i < $table->player_n ; $i++) {
2117 $user_cur = $this->user[$table->player[$i]];
2119 $flags = $user_cur->flags;
2121 if ($user->idx_get() == $table->player[$i])
2124 log_main($user_cur->name. sprintf(" IN TABLE [%d]", $table_idx));
2126 $content .= sprintf('%s[ %d, "%s" ]',($i == 0 ? '' : ', '), $flags, xcape($user_cur->name));
2131 $ret .= sprintf('j_tab_cont(%d, %s);', $table_idx, $content);
2136 static function garbage_time_is_expired($tm)
2141 if (file_exists(PROXY_PATH) == FALSE)
2142 mkdir(PROXY_PATH, 0775, TRUE);
2143 if (($fp = @fopen(PROXY_PATH."/garbage_time.expired", 'rb')) == FALSE)
2145 if (($s = fread($fp, 4)) == FALSE)
2147 if (mb_strlen($s, "ASCII") != 4)
2149 $arr = unpack('Le', $s);
2150 if ($arr['e'] > $tm)
2157 log_rd2("END: return ".($ret ? "TRUE" : "FALSE"));
2162 static function garbage_time_expire_set($tm)
2165 if (file_exists(PROXY_PATH) == FALSE)
2166 mkdir(PROXY_PATH, 0775, TRUE);
2167 if (($fp = @fopen(PROXY_PATH."/garbage_time.expired", 'wb')) == FALSE)
2169 fwrite($fp, pack("L",$tm));
2178 function request_mgr(&$s_a_p, &$header_out, &$new_socket, $path, $addr, $get, $post, $cookie)
2180 printf("NEW_SOCKET (root): %d\n", intval($new_socket));
2182 force_no_cache($header_out);
2188 index_main($this, $header_out, $addr, $get, $post, $cookie);
2189 $content = ob_get_contents();
2192 $s_a_p->pgflush_try_add($new_socket, 20, $header_out, $content);
2196 case "index_wr.php":
2198 index_wr_main($this, $addr, $get, $post, $cookie);
2199 $content = ob_get_contents();
2202 $s_a_p->pgflush_try_add($new_socket, 20, $header_out, $content);
2206 case "index_rd_ifra.php":
2208 if (!isset($cookie['sess'])
2209 || (($user = $this->get_user($cookie['sess'], $idx)) == FALSE)) {
2210 $content = User::stream_fini(TRUE);
2212 $s_a_p->pgflush_try_add($new_socket, 20, $header_out, $content);
2217 // close a previous opened index_read_ifra socket, if exists
2218 if (($prev = $user->rd_socket_get()) != NULL) {
2219 $s_a_p->socks_unset($user->rd_socket_get());
2220 fclose($user->rd_socket_get());
2221 printf("CLOSE AND OPEN AGAIN ON IFRA2\n");
2222 $user->rd_socket_set(NULL);
2226 $user->stream_init($header_out, $content, $get, $post, $cookie);
2228 $response = headers_render($header_out, -1).chunked_content($content);
2229 $response_l = mb_strlen($response, "ASCII");
2231 $wret = @fwrite($new_socket, $response, $response_l);
2232 if ($wret < $response_l) {
2233 printf("TROUBLES WITH FWRITE: %d\n", $wret);
2234 $user->rd_cache_set(mb_substr($content, $wret, $response_l - $wret, "ASCII"));
2237 $user->rd_cache_set("");
2239 fflush($new_socket);
2242 $s_a_p->socks_set($new_socket, $user);
2243 $user->rd_socket_set($new_socket);
2244 printf(" - qui ci siamo - ");
2252 /* FAR TODO: move all into an array of registered sub-apps */
2253 $subs = "briskin5/";
2254 $subs_l = strlen($subs);
2255 if (!strncmp($path, $subs, $subs_l)) {
2256 $ret = Bin5::request_mgr(&$s_a_p, &$header_out, &$new_socket, substr($path, $subs_l) , $addr, $get, $post, $cookie);
2265 function match_add($idx, $match)
2267 $this->match[$idx] = $match;
2270 function match_del($idx)
2272 unset($this->match[$idx]);
2275 function match_get($idx, $token)
2277 if (isset($this->match[$idx])) {
2279 || $token == $this->match[$idx]->table_token) {
2280 return ($this->match[$idx]);
2288 function make_seed()
2290 list($usec, $sec) = explode(' ', microtime());
2291 return (float) $sec + ((float) $usec * 100000);
2294 function btrace_line($ar)
2296 GLOBAL $G_btrace_pref_sub;
2299 for ($i = 0 ; $i < count($ar) ; $i++) {
2300 $with_class = isset($ar[$i]['class']);
2301 $with_file = isset($ar[$i]['file']);
2302 $ret .= sprintf("%s%s%s (%s:%d)", ($i == 0 ? "" : ", "),
2303 ($with_class ? $ar[$i]['class'].$ar[$i]['type'] : ""),
2304 $ar[$i]['function'], ($with_file ? str_replace($G_btrace_pref_sub, "", $ar[$i]['file']) : ""),
2305 ($with_file ? $ar[$i]['line'] : ""));
2311 function trace_ftok($id, $add)
2313 // NOTE: without space to use sed to substitute "= @ftok(" with "= @ftok("
2314 $tok=@ftok($id, $add);
2316 log_shme($tok.": ".$id." + ".$add);
2321 function log_mop($step, $log)
2323 GLOBAL $sess, $PHP_SELF;
2325 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LMOP) == 0)
2328 if (isset($sess) == FALSE)
2333 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LMOP) == 0)
2336 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2337 $btrace = btrace_line(debug_backtrace());
2340 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2341 fwrite($fp, sprintf("LMOP: [%f] [%05d] [%s] [%s]\n", gettimeofday(TRUE), $step, $log, $btrace));
2347 function log_only2($log)
2349 GLOBAL $sess, $PHP_SELF;
2351 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONL2) == 0)
2354 if (isset($sess) == FALSE)
2359 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONL2) == 0)
2362 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2363 $btrace = btrace_line(debug_backtrace());
2366 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2367 fwrite($fp, sprintf("ONL2: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2372 function log_crit($log)
2374 GLOBAL $sess, $PHP_SELF;
2376 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_CRIT) == 0)
2379 if (isset($sess) == FALSE)
2384 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_CRIT) == 0)
2387 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2388 $btrace = btrace_line(debug_backtrace());
2391 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2392 fwrite($fp, sprintf("CRIT: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2397 function log_only($log)
2399 GLOBAL $sess, $PHP_SELF;
2401 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_ONLY) == 0)
2404 if (isset($sess) == FALSE)
2409 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_ONLY) == 0)
2412 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2413 $btrace = btrace_line(debug_backtrace());
2416 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2417 fwrite($fp, sprintf("ONLY: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2422 function log_main($log)
2424 GLOBAL $sess, $PHP_SELF;
2426 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_MAIN) == 0)
2429 if (isset($sess) == FALSE)
2434 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_MAIN) == 0)
2437 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2438 $btrace = btrace_line(debug_backtrace());
2441 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2442 fwrite($fp, sprintf("MAIN: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2447 function log_rd($log)
2449 GLOBAL $sess, $PHP_SELF;
2451 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_READ) == 0)
2454 if (isset($sess) == FALSE)
2459 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_READ) == 0)
2462 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2463 $btrace = btrace_line(debug_backtrace());
2466 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2467 fwrite($fp, sprintf("READ: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2472 function log_rd2($log)
2474 GLOBAL $sess, $PHP_SELF;
2476 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_REA2) == 0)
2479 if (isset($sess) == FALSE)
2484 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_REA2) == 0)
2487 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2488 $btrace = btrace_line(debug_backtrace());
2492 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2493 fwrite($fp, sprintf("REA2: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2498 function log_send($log)
2500 GLOBAL $sess, $PHP_SELF;
2502 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SEND) == 0)
2505 if (isset($sess) == FALSE)
2510 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SEND) == 0)
2513 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2514 $btrace = btrace_line(debug_backtrace());
2517 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2518 fwrite($fp, sprintf("SEND: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2523 function log_lock($log)
2525 GLOBAL $sess, $PHP_SELF;
2527 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOCK) == 0)
2530 if (isset($sess) == FALSE)
2535 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOCK) == 0)
2538 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2539 $btrace = btrace_line(debug_backtrace());
2542 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2543 fwrite($fp, sprintf("LOCK: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2548 function log_wr($log)
2550 GLOBAL $sess, $PHP_SELF;
2552 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_WRIT) == 0)
2555 if (isset($sess) == FALSE)
2560 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_WRIT) == 0)
2563 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2564 $btrace = btrace_line(debug_backtrace());
2567 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2568 fwrite($fp, sprintf("WRIT: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2573 function log_load($log)
2575 GLOBAL $sess, $PHP_SELF;
2577 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_LOAD) == 0)
2580 if (isset($sess) == FALSE)
2585 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_LOAD) == 0)
2588 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2589 $btrace = btrace_line(debug_backtrace());
2592 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2593 fwrite($fp, sprintf("LOAD: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2598 function log_auth($sess, $log)
2602 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_AUTH) == 0)
2605 if (( (BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_AUTH) == 0)
2608 if ((BRISK_DEBUG | ($sess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2609 $btrace = btrace_line(debug_backtrace());
2612 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2613 fwrite($fp, sprintf("LOAD: [%s] [%d] [%s] [%s]\n", $sess, time(), $log, $btrace));
2618 function log_shme($log)
2620 GLOBAL $sess, $PHP_SELF;
2622 if (BRISK_SINGLE_SESS == "" && (BRISK_DEBUG & DBG_SHME) == 0)
2625 if (isset($sess) == FALSE)
2630 if (( (BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_SHME) == 0)
2633 if ((BRISK_DEBUG | ($ssess == BRISK_SINGLE_SESS ? BRISK_SINGLE_DEBUG : 0) ) & DBG_TRAC)
2634 $btrace = btrace_line(debug_backtrace());
2637 if (($fp = @fopen(LEGAL_PATH."/brisk.log", 'a')) != FALSE) {
2638 fwrite($fp, sprintf("SHME: [%s] [%s] [%s]\n", $ssess, $log, $btrace));
2645 // function log_legal($curtime, $sess, $name, $where, $mesg)
2646 function log_legal($curtime, $addr, $user, $where, $mesg)
2649 if (($fp = @fopen(LEGAL_PATH."/legal.log", 'a')) != FALSE) {
2650 /* Unix time | session | nickname | IP | where was | mesg */
2651 fwrite($fp, sprintf("%ld|%s|%s|%s|%s|%s|%s|\n", $curtime, $user->sess,
2652 ($user->flags & USER_FLAG_AUTH ? 'A' : 'N'),
2653 $user->name, $addr, $where , $mesg));
2658 function table_act_content($isstanding, $sitted, $table, $cur_table, $allowed)
2663 if ($sitted < PLAYERS_N) {
2671 if ($table == $cur_table)
2678 $ret = sprintf('j_tab_act_cont(%d, \'%s\');', $table, $act);
2683 function show_notify($text, $tout, $butt, $w, $h)
2685 log_main("SHOW_NOTIFY: ".$text);
2686 return sprintf('var noti = new notify(gst,"%s",%d,"%s",%d,%d);', $text, $tout, $butt, $w, $h);
2689 function show_notify_ex($text, $tout, $butt, $w, $h, $is_opaque, $block_time)
2691 log_main("SHOW_NOTIFY OPAQUE: ".$text);
2692 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);
2696 function root_wellcome($user)
2698 GLOBAL $root_wellarr, $G_lang;
2702 $dt = date("H:i ", $curtime);
2704 for ($i = 0 ; $i < count($root_wellarr[$G_lang]) ; $i++)
2705 $ret .= sprintf('chatt_sub("%s", [2, "%s"],"%s");', $dt, NICKSERV, str_replace('"', '\"', $root_wellarr[$G_lang][$i]));
2712 function validate_sess($sess)
2714 if (strlen($sess) == SESS_LEN)
2720 function validate_name($name)
2722 $name_new = str_replace(' ', '_', substr(trim($name),0,12));
2724 for ($i = 0 ; $i < strlen($name_new) ; $i++) {
2726 if (($c >= "a" && $c <= "z") || ($c >= "A" && $c <= "Z") || ($c >= "0" && $c <= "9"))
2733 function playsound($filename)
2735 return (sprintf('playsound("flasou", "%s");', $filename));
2738 function secstoword($secs)
2744 $mins = floor($secs / 60);
2746 if ($G_lang == 'en') {
2748 $ret = sprintf("%d minute%s%s", $mins, ($mins > 1 ? "s" : ""), ($secs > 0 ? " and " : ""));
2751 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "s" : ""));
2755 $ret = sprintf("%d minut%s%s", $mins, ($mins > 1 ? "i" : "o"), ($secs > 0 ? " e " : ""));
2758 $ret .= sprintf("%d second%s", $secs, ($secs > 1 ? "i" : "o"));
2763 function sharedmem_sz($tok)
2765 if (($shm_id = @shmop_open($tok, 'a', 0, 0)) == FALSE) {
2766 log_main("shmop_open failed");
2769 $shm_sz = shmop_size($shm_id);
2770 shmop_close($shm_id);
2772 // log_main("shm_sz: ".$shm_sz." SHM_DIMS: ".SHM_DIMS);
2779 static function lock_data($is_exclusive)
2781 if (($res = file_lock(FTOK_PATH."/warrant", $is_exclusive)) != FALSE) {
2782 self::$delta_t = microtime(TRUE);
2783 log_lock("LOCK warrant [".self::$delta_t."]");
2791 static function unlock_data($res)
2795 log_lock("UNLOCK warrant [".(microtime(TRUE) - (self::$delta_t))."]");
2804 static function lock_data($is_exclusive)
2806 if (($res = file_lock(FTOK_PATH."/poll", $is_exclusive)) != FALSE) {
2807 self::$delta_t = microtime(TRUE);
2808 log_lock("LOCK poll [".self::$delta_t."]");
2816 static function unlock_data($res)
2820 log_lock("UNLOCK poll [".(microtime(TRUE) - (self::$delta_t))."]");