corretta gestione nomi con lettere strane
[brisk.git] / web / index_wr.php
1 <?php
2 /*
3  *  brisk - index_wr.php
4  *
5  *  Copyright (C) 2006-2008 Matteo Nastasi
6  *                          mailto: nastasi@alternativeoutput.it 
7  *                                  matteo.nastasi@milug.org
8  *                          web: http://www.alternativeoutput.it
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details. You should have received a
19  * copy of the GNU General Public License along with this program; if
20  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
21  * Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * $Id$
24  *
25  */
26
27 require_once("Obj/brisk.phh");
28 require_once("Obj/auth.phh");
29 // require_once("Obj/proxyscan.phh");
30 require_once("briskin5/Obj/briskin5.phh");
31
32 // Use of proxies isn't allowed.
33 // if (is_proxy()) {
34 //   sleep(5);
35 //   exit;
36 // }
37 log_load("index_wr.php");
38
39 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
40   echo "Debugging time!";
41   exit;
42 }
43
44 /*
45  *  MAIN
46  */
47
48 /* if the IP is banned, exit without do nothing */
49 if (array_search($_SERVER['REMOTE_ADDR'], $G_black_list) !== FALSE) {
50   sleep(5);
51   exit;
52 }
53
54 $is_spawn = FALSE;
55
56 log_wr('COMM: '.$mesg);
57
58 $sem = Room::lock_data();
59 if (($room = &Room::load_data()) == FALSE) {
60   echo "Load data error";
61   log_wr("Load data error");
62   Room::unlock_data($sem);
63   exit;
64 }
65 if (($user = &$room->get_user($sess, &$idx)) == FALSE) {
66   Room::unlock_data($sem);
67   $argz = explode('|', $mesg);
68
69   if ($argz[0] == 'getchallenge') {
70     GLOBAL $cli_name;
71     if (($a_sem = Challenges::lock_data()) != FALSE) { 
72       log_main("chal lock data success");
73       
74       if (($chals = &Challenges::load_data()) != FALSE) {
75         $curtime = time();
76
77         $token =  uniqid("");
78         // echo '2|'.$argz[1].'|'.$token.'|'.$_SERVER['REMOTE_ADDR'].'|'.$curtime.'|';
79         // exit;
80
81         if (($login_new = validate_name(urldecode($cli_name))) != FALSE) {
82           if ($chals->add($login_new, $token, $_SERVER['REMOTE_ADDR'], $curtime) != FALSE) {
83             echo '0|'.$token;
84           }
85           else {
86             echo '1|';
87           }
88         }
89         else {
90           echo '1|';
91         }
92         if ($chals->ismod()) {
93           Challenges::save_data(&$chals);
94         }
95       }
96       
97
98       Challenges::unlock_data($a_sem);
99     }
100   }
101   else if ($argz[0] == 'auth') {
102     printf("challenge|ok");
103   }
104   else if ($argz[0] == 'help') {
105     echo show_notify(str_replace("\n", " ", $G_room_help), 0, "torna ai tavoli", 600, 500);
106   }
107   else if ($argz[0] == 'about') {
108     echo show_notify(str_replace("\n", " ", $G_room_about), 0, "torna ai tavoli", 400, 200);
109   }
110   else if ($argz[0] == 'roadmap') {
111     echo show_notify(str_replace("\n", " ", $G_room_roadmap), 0, "torna ai tavoli", 400, 200);
112   }
113   else if ($argz[0] == 'whysupport') {
114     echo show_notify(str_replace("\n", " ", $G_room_whysupport), 0, "torna ai tavoli", 400, 200);
115   }
116   else { 
117     log_wr("Get User Error");
118     echo "Get User Error:" + $argz[0];
119   }
120   exit;
121 }
122 $argz = explode('|', $mesg);
123
124 log_wr('POSTSPLIT: '.$argz[0]);
125
126 if ($argz[0] == 'shutdown') {
127   log_auth($user->sess, "Shutdown session.");
128
129   $user->reset();
130
131   log_rd2("AUTO LOGOUT.");
132   if ($user->subst == 'sitdown' || $user->stat == 'table')
133     $room->room_wakeup(&$user);
134   else if ($user->subst == 'standup')
135     $room->room_outstandup(&$user);
136   else
137     log_rd2("SHUTDOWN FROM WHAT ???");
138 }
139 else if ($argz[0] == 'warranty') {
140   GLOBAL $cli_name, $cli_email;
141
142   $curtime = time();
143   $mesg_to_user = "";
144
145   log_wr("INFO:SKIP:argz == warranty name: [".$cli_name."] AUTH: ".($user->flags & USER_FLAG_AUTH));
146   if ($user->flags & USER_FLAG_AUTH) {
147     if (($wa_lock = Warrant::lock_data()) != FALSE) {
148       if (($fp = @fopen(LEGAL_PATH."/warrant.txt", 'a')) != FALSE) {
149         /* Unix time | session | nickname | IP | where was | mesg */
150         fwrite($fp, sprintf("%ld|%s|%s|%s|\n", $curtime, $user->name, xcapelt(urldecode($cli_name)), xcapelt(urldecode($cli_email))));
151         fclose($fp);
152       }
153       Warrant::unlock_data($wa_lock);
154       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
155       $user->comm[$user->step % COMM_N] .=  show_notify("<br>Il nominativo &egrave; stato inoltrato all\'amministratore.<br><br>Nell\'arco di pochi giorni vi verr&agrave;<br><br>notificata l\'avvenuta registrazione.", 0, "chiudi", 400, 150);
156       $user->step_inc();
157       echo "1";
158     }
159     else {
160       $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"<b>E\' occorso un errore durante il salvataggio, riprova o contatta l\'amministratore.</b>");', $dt, NICKSERV);
161     }
162     
163   }
164   else {
165     $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>");', $dt, NICKSERV);
166   }
167
168   if ($mesg_to_user != "") {
169     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
170     
171     $dt = date("H:i ", $curtime);
172     $user->comm[$user->step % COMM_N] .= $mesg_to_user;
173     $user->step_inc();
174   }
175 }
176 /******************
177  *                *
178  *   STAT: room   *
179  *                *
180  ******************/
181 else if ($user->stat == 'room') {
182   $user->laccwr = time();
183
184   if ($argz[0] == 'help') {
185     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
186     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_help), 0, "torna ai tavoli", 600, 500);
187
188     log_wr($user->comm[$user->step % COMM_N]);
189     $user->step_inc();
190     
191   }
192   else if ($argz[0] == 'about') {
193     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
194     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_about), 0, "torna ai tavoli", 400, 200);
195
196     log_wr($user->comm[$user->step % COMM_N]);
197     $user->step_inc();
198     
199   }
200   else if ($argz[0] == 'roadmap') {
201     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
202     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_roadmap), 0, "torna ai tavoli", 400, 200);
203
204     log_wr($user->comm[$user->step % COMM_N]);
205     $user->step_inc();
206     
207   }
208   else if ($argz[0] == 'whysupport') {
209     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
210     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_whysupport), 0, "torna ai tavoli", 400, 200);
211
212     log_wr($user->comm[$user->step % COMM_N]);
213     $user->step_inc();
214     
215   }
216   else if ($argz[0] == 'chatt') {
217     $room->chatt_send(&$user,$mesg);
218   }
219   /**********************
220    *                    *
221    *   SUBST: standup   *
222    *                    *
223    **********************/
224   else if ($user->subst == 'standup') {
225    
226     if ($argz[0] == 'sitdown') {
227       log_wr("SITDOWN command");
228
229       if ($user->the_end == TRUE) {
230         log_wr("INFO:SKIP:argz == sitdown && the_end == TRUE => ignore request.");
231         Room::unlock_data($sem);
232         exit;
233       }
234
235       // Take parameters
236       $table_idx = $argz[1];
237       $table = &$room->table[$table_idx];
238     
239       $curtime = time();
240
241       if ($G_shutdown || $table->wakeup_time > $curtime || 
242           ($table->auth_only && (($user->flags & USER_FLAG_AUTH) == 0)) ) {
243         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
244
245         $dt = date("H:i ", $curtime);
246         if ($G_shutdown) {
247           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>Il server sta per essere riavviato, non possono avere inizio nuove partite.</b>");', $dt, NICKSERV);
248         }
249         else if ($table->auth_only && (($user->flags & USER_FLAG_AUTH) == 0)) {
250           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>Il tavolo a cui volevi sederti richiede autentifica.</b>");', $dt, NICKSERV);
251         }
252         else {
253           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>Il tavolo si &egrave; appena liberato, ci si potr&agrave; sedere tra %d secondi.</b>");', $dt, NICKSERV, $table->wakeup_time - $curtime);
254         }
255         $user->step_inc();
256         Room::save_data($room);
257         Room::unlock_data($sem);
258         exit;
259       }
260
261       /* TODO: refact to a function */
262       // if ($user->bantime > $user->laccwr) {
263       require_once("Obj/hardban.phh");
264
265       if (($bantime = Hardbans::check(($user->flags & USER_FLAG_AUTH ? $user->name : FALSE),
266                           $user->ip, $user->sess)) != -1) {
267         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
268         if ($user->flags & USER_FLAG_AUTH) {
269           $user->comm[$user->step % COMM_N] .= show_notify("<br>Ti sei alzato da un tavolo senza il consenso degli altri giocatori. <br><br>Dovrai aspettare ancora ".secstoword($user->bantime - $user->laccwr)." prima di poterti sedere nuovamente.", 2000, "resta in piedi.", 400, 100);
270         }
271         else {
272           $user->comm[$user->step % COMM_N] .= show_notify("<br>Tu o qualcuno col tuo stesso indirizzo IP si รจ alzato da un tavolo senza il consenso degli altri giocatori.<br><br>Dovrai aspettare ancora ".secstoword($bantime - $user->laccwr)." prima di poterti sedere nuovamente.<br><br>Se non sei stato tu ad alzarti e possiedi un login con password, autenticandoti con quello, potrai accedere.", 2000, "resta in piedi.", 400, 180);
273         }
274         $user->step_inc();
275         Room::save_data($room);
276         Room::unlock_data($sem);
277         exit;
278       }
279     
280       if ($table->player_n == PLAYERS_N) {
281         log_wr("WARN:FSM: Sitdown unreachable, table full.");
282         Room::unlock_data($sem);
283         exit;
284       } 
285       
286       // set new status
287       $user->subst = "sitdown";
288       $user->table = $table_idx;
289       $user->table_pos = $table->user_add($idx);
290       
291       log_wr("MOP before");
292
293       if ($table->player_n == PLAYERS_N) {
294         log_wr("MOP inall");
295
296         // Start game for this table.
297         log_wr("Start game!");
298         
299         //
300         //  START THE SPAWN HERE!!!!
301         //
302
303         $curtime = time();
304
305         // Create new spawned table
306         $bri_sem = Briskin5::lock_data($table_idx);
307         $table_token = uniqid("");
308         $room->table[$table_idx]->table_token = $table_token;
309         $room->table[$table_idx]->table_start = $curtime;
310         
311         $plist = "$table_token|$user->table|$table->player_n";
312         for ($i = 0 ; $i < $table->player_n ; $i++) {
313           $plist .= '|'.$room->user[$table->player[$i]]->sess;
314         }
315         log_legal($curtime, $user->sess, $user->name, "STAT:CREATE_GAME", $plist);
316
317         if (($bri =& new Briskin5(&$room, $table_idx, $table_token)) == FALSE)
318           log_wr("bri create: FALSE");
319         else
320           log_wr("bri create: ".serialize($bri));
321         
322         // init table
323         $bri_table =& $bri->table[0];
324         $bri_table->init(&$bri->user);
325         $bri_table->game_init(&$bri->user);
326         //
327         // Init spawned users.
328         //
329         for ($i = 0 ; $i < $table->player_n ; $i++) {
330           $bri_user_cur = &$bri->user[$i];
331           $user_cur = &$room->user[$table->player[$i]];
332           
333           $bri_user_cur->stat_set('table');
334           $bri_user_cur->subst = 'asta';
335           $bri_user_cur->laccwr = $curtime;
336           
337           $bri_user_cur->trans_step = $user_cur->step + 1;
338           $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = "";
339           $bri_user_cur->step_inc();
340           $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = show_table(&$bri,&$bri_user_cur,$bri_user_cur->step+1,TRUE, FALSE);
341           
342           $bri_user_cur->step_inc();
343           
344           log_wr("TRY PRESAVE: ".$bri_user_cur->step." TRANS STEP: ".$bri_user_cur->trans_step);
345           
346           log_wr("Pre if!");
347           
348           //          ARRAY_POP DISABLED
349           //        // CHECK
350           while (array_pop($user_cur->comm) != NULL);
351           
352           $ret = "";
353           $ret .= sprintf('gst.st_loc++; gst.st=%d; createCookie("table_idx", %d, 24*365, cookiepath); createCookie("table_token", "%s", 24*365, cookiepath); the_end=true; window.onunload = null ; window.onbeforeunload = null ; document.location.assign("briskin5/index.php");|', $user_cur->step+1, $table_idx, $table_token);
354           
355           $user_cur->comm[$user_cur->step % COMM_N] = $ret;
356           $user_cur->trans_step = $user_cur->step + 1;
357           log_wr("TRANS ATTIVATO");
358           
359           
360           $user_cur->stat_set('table');
361           $user_cur->subst = 'asta';
362           $user_cur->laccwr = $curtime;
363           $user_cur->step_inc();
364         }
365         log_wr("presave bri");
366         Briskin5::save_data($bri);
367         Briskin5::unlock_data($bri_sem);
368         log_wr("postsave bri");
369       }
370       // change room
371       $room->room_sitdown(&$user, $table_idx);
372
373       log_wr("MOP finish");
374
375       
376     }
377     else if ($argz[0] == 'logout') {
378       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
379       $user->comm[$user->step % COMM_N] .= 'postact_logout();';
380       $user->the_end = TRUE;
381       $user->step_inc();
382     }
383   }
384   /**********************
385    *                    *
386    *   SUBST: sitdown   *
387    *                    *
388    **********************/
389   else if ($user->subst == 'sitdown') {
390     if ($argz[0] == 'wakeup') {
391       $room->room_wakeup(&$user);      
392     }
393     else if ($argz[0] == 'logout') {
394       $room->room_wakeup(&$user);      
395       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
396       $user->comm[$user->step % COMM_N] .= 'postact_logout();';
397       $user->the_end = TRUE;
398       $user->step_inc();
399     }
400   }
401 }
402 log_wr("before save data");
403 Room::save_data($room);
404
405 Room::unlock_data($sem);
406 exit;
407 ?>