hardbans added
[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     if (($a_sem = Challenges::lock_data()) != FALSE) { 
71       log_main("chal lock data success");
72       
73       if (($chals = &Challenges::load_data()) != FALSE) {
74         $curtime = time();
75
76         $token =  uniqid("");
77         // echo '2|'.$argz[1].'|'.$token.'|'.$_SERVER['REMOTE_ADDR'].'|'.$curtime.'|';
78         // exit;
79
80         if (($login_new = validate_name($argz[1])) != FALSE) {
81           if ($chals->add($login_new, $token, $_SERVER['REMOTE_ADDR'], $curtime) != FALSE) {
82             echo '0|'.$token;
83           }
84           else {
85             echo '1|';
86           }
87         }
88         else {
89           echo '1|';
90         }
91         if ($chals->ismod()) {
92           Challenges::save_data(&$chals);
93         }
94       }
95       
96
97       Challenges::unlock_data($a_sem);
98     }
99   }
100   else if ($argz[0] == 'auth') {
101     printf("challenge|ok");
102   }
103   else if ($argz[0] == 'help') {
104     echo show_notify(str_replace("\n", " ", $G_room_help), 0, "torna ai tavoli", 600, 500);
105   }
106   else if ($argz[0] == 'about') {
107     echo show_notify(str_replace("\n", " ", $G_room_about), 0, "torna ai tavoli", 400, 200);
108   }
109   else if ($argz[0] == 'roadmap') {
110     echo show_notify(str_replace("\n", " ", $G_room_roadmap), 0, "torna ai tavoli", 400, 200);
111   }
112   else if ($argz[0] == 'whysupport') {
113     echo show_notify(str_replace("\n", " ", $G_room_whysupport), 0, "torna ai tavoli", 400, 200);
114   }
115   else { 
116     log_wr("Get User Error");
117     echo "Get User Error:" + $argz[0];
118   }
119   exit;
120 }
121 $argz = explode('|', $mesg);
122
123 log_wr('POSTSPLIT: '.$argz[0]);
124
125 if ($argz[0] == 'shutdown') {
126   log_auth($user->sess, "Shutdown session.");
127
128   $user->reset();
129
130   log_rd2("AUTO LOGOUT.");
131   if ($user->subst == 'sitdown' || $user->stat == 'table')
132     $room->room_wakeup(&$user);
133   else if ($user->subst == 'standup')
134     $room->room_outstandup(&$user);
135   else
136     log_rd2("SHUTDOWN FROM WHAT ???");
137 }
138 /******************
139  *                *
140  *   STAT: room   *
141  *                *
142  ******************/
143 else if ($user->stat == 'room') {
144   $user->laccwr = time();
145
146   if ($argz[0] == 'help') {
147     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
148     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_help), 0, "torna ai tavoli", 600, 500);
149
150     log_wr($user->comm[$user->step % COMM_N]);
151     $user->step_inc();
152     
153   }
154   else if ($argz[0] == 'about') {
155     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
156     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_about), 0, "torna ai tavoli", 400, 200);
157
158     log_wr($user->comm[$user->step % COMM_N]);
159     $user->step_inc();
160     
161   }
162   else if ($argz[0] == 'roadmap') {
163     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
164     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_roadmap), 0, "torna ai tavoli", 400, 200);
165
166     log_wr($user->comm[$user->step % COMM_N]);
167     $user->step_inc();
168     
169   }
170   else if ($argz[0] == 'whysupport') {
171     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
172     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_whysupport), 0, "torna ai tavoli", 400, 200);
173
174     log_wr($user->comm[$user->step % COMM_N]);
175     $user->step_inc();
176     
177   }
178   else if ($argz[0] == 'chatt') {
179     $room->chatt_send(&$user,$mesg);
180   }
181   /**********************
182    *                    *
183    *   SUBST: standup   *
184    *                    *
185    **********************/
186   else if ($user->subst == 'standup') {
187    
188     if ($argz[0] == 'sitdown') {
189       log_wr("SITDOWN command");
190
191       if ($user->the_end == TRUE) {
192         log_wr("INFO:SKIP:argz == sitdown && the_end == TRUE => ignore request.");
193         Room::unlock_data($sem);
194         exit;
195       }
196
197       // Take parameters
198       $table_idx = $argz[1];
199       $table = &$room->table[$table_idx];
200     
201       $curtime = time();
202
203       if ($G_shutdown || $table->wakeup_time > $curtime || 
204           ($table->auth_only && (($user->flags & USER_FLAG_AUTH) == 0)) ) {
205         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
206
207         $dt = date("H:i ", $curtime);
208         if ($G_shutdown) {
209           $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);
210         }
211         else if ($table->auth_only && (($user->flags & USER_FLAG_AUTH) == 0)) {
212           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>Il tavolo a cui volevi sederti richiede autentifica.</b>");', $dt, NICKSERV);
213         }
214         else {
215           $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);
216         }
217         $user->step_inc();
218         Room::save_data($room);
219         Room::unlock_data($sem);
220         exit;
221       }
222
223       /* TODO: refact to a function */
224       // if ($user->bantime > $user->laccwr) {
225       require_once("Obj/hardban.phh");
226
227       if (($bantime = Hardbans::check(($user->flags & USER_FLAG_AUTH ? $user->name : FALSE),
228                           $user->ip, $user->sess)) != -1) {
229         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
230         if ($user->flags & USER_FLAG_AUTH) {
231           $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);
232         }
233         else {
234           $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);
235         }
236         $user->step_inc();
237         Room::save_data($room);
238         Room::unlock_data($sem);
239         exit;
240       }
241     
242       if ($table->player_n == PLAYERS_N) {
243         log_wr("WARN:FSM: Sitdown unreachable, table full.");
244         Room::unlock_data($sem);
245         exit;
246       } 
247       
248       // set new status
249       $user->subst = "sitdown";
250       $user->table = $table_idx;
251       $user->table_pos = $table->user_add($idx);
252       
253       log_wr("MOP before");
254
255       if ($table->player_n == PLAYERS_N) {
256         log_wr("MOP inall");
257
258         // Start game for this table.
259         log_wr("Start game!");
260         
261         //
262         //  START THE SPAWN HERE!!!!
263         //
264
265         $curtime = time();
266
267         // Create new spawned table
268         $bri_sem = Briskin5::lock_data($table_idx);
269         $table_token = uniqid("");
270         $room->table[$table_idx]->table_token = $table_token;
271         $room->table[$table_idx]->table_start = $curtime;
272         
273         $plist = "$table_token|$user->table|$table->player_n";
274         for ($i = 0 ; $i < $table->player_n ; $i++) {
275           $plist .= '|'.$room->user[$table->player[$i]]->sess;
276         }
277         log_legal($curtime, $user->sess, $user->name, "STAT:CREATE_GAME", $plist);
278
279         if (($bri =& new Briskin5(&$room, $table_idx, $table_token)) == FALSE)
280           log_wr("bri create: FALSE");
281         else
282           log_wr("bri create: ".serialize($bri));
283         
284         // init table
285         $bri_table =& $bri->table[0];
286         $bri_table->init(&$bri->user);
287         $bri_table->game_init(&$bri->user);
288         //
289         // Init spawned users.
290         //
291         for ($i = 0 ; $i < $table->player_n ; $i++) {
292           $bri_user_cur = &$bri->user[$i];
293           $user_cur = &$room->user[$table->player[$i]];
294           
295           $bri_user_cur->stat_set('table');
296           $bri_user_cur->subst = 'asta';
297           $bri_user_cur->laccwr = $curtime;
298           
299           $bri_user_cur->trans_step = $user_cur->step + 1;
300           $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = "";
301           $bri_user_cur->step_inc();
302           $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = show_table(&$bri,&$bri_user_cur,$bri_user_cur->step+1,TRUE, FALSE);
303           
304           $bri_user_cur->step_inc();
305           
306           log_wr("TRY PRESAVE: ".$bri_user_cur->step." TRANS STEP: ".$bri_user_cur->trans_step);
307           
308           log_wr("Pre if!");
309           
310           //          ARRAY_POP DISABLED
311           //        // CHECK
312           while (array_pop($user_cur->comm) != NULL);
313           
314           $ret = "";
315           $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);
316           
317           $user_cur->comm[$user_cur->step % COMM_N] = $ret;
318           $user_cur->trans_step = $user_cur->step + 1;
319           log_wr("TRANS ATTIVATO");
320           
321           
322           $user_cur->stat_set('table');
323           $user_cur->subst = 'asta';
324           $user_cur->laccwr = $curtime;
325           $user_cur->step_inc();
326         }
327         log_wr("presave bri");
328         Briskin5::save_data($bri);
329         Briskin5::unlock_data($bri_sem);
330         log_wr("postsave bri");
331       }
332       // change room
333       $room->room_sitdown(&$user, $table_idx);
334
335       log_wr("MOP finish");
336
337       
338     }
339     else if ($argz[0] == 'logout') {
340       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
341       $user->comm[$user->step % COMM_N] .= 'postact_logout();';
342       $user->the_end = TRUE;
343       $user->step_inc();
344     }
345   }
346   /**********************
347    *                    *
348    *   SUBST: sitdown   *
349    *                    *
350    **********************/
351   else if ($user->subst == 'sitdown') {
352     if ($argz[0] == 'wakeup') {
353       $room->room_wakeup(&$user);      
354     }
355     else if ($argz[0] == 'logout') {
356       $room->room_wakeup(&$user);      
357       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
358       $user->comm[$user->step % COMM_N] .= 'postact_logout();';
359       $user->the_end = TRUE;
360       $user->step_inc();
361     }
362   }
363 }
364 log_wr("before save data");
365 Room::save_data($room);
366
367 Room::unlock_data($sem);
368 exit;
369 ?>