lang cookie 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     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     /* MLANG: "torna ai tavoli" */ 
106     echo show_notify(str_replace("\n", " ", $G_room_help), 0, "torna ai tavoli", 600, 500);
107   }
108   else if ($argz[0] == 'about') {
109     echo show_notify(str_replace("\n", " ", $G_room_about), 0, "torna ai tavoli", 400, 200);
110   }
111   else if ($argz[0] == 'roadmap') {
112     echo show_notify(str_replace("\n", " ", $G_room_roadmap), 0, "torna ai tavoli", 400, 200);
113   }
114   else if ($argz[0] == 'whysupport') {
115     echo show_notify(str_replace("\n", " ", $G_room_whysupport), 0, "torna ai tavoli", 400, 200);
116   }
117   else { 
118     log_wr("Get User Error");
119     echo "Get User Error:" + $argz[0];
120   }
121   exit;
122 }
123 $argz = explode('|', $mesg);
124
125 log_wr('POSTSPLIT: '.$argz[0]);
126
127 if ($argz[0] == 'shutdown') {
128   log_auth($user->sess, "Shutdown session.");
129
130   $user->reset();
131
132   log_rd2("AUTO LOGOUT.");
133   if ($user->subst == 'sitdown' || $user->stat == 'table')
134     $room->room_wakeup(&$user);
135   else if ($user->subst == 'standup')
136     $room->room_outstandup(&$user);
137   else {
138     log_rd2("SHUTDOWN FROM WHAT ???");
139   }
140 }
141 else if ($argz[0] == 'warranty') {
142   GLOBAL $cli_name, $cli_email;
143
144   $curtime = time();
145   $mesg_to_user = "";
146
147   log_wr("INFO:SKIP:argz == warranty name: [".$cli_name."] AUTH: ".($user->flags & USER_FLAG_AUTH));
148   if ($user->flags & USER_FLAG_AUTH) {
149     if (($wa_lock = Warrant::lock_data()) != FALSE) {
150       if (($fp = @fopen(LEGAL_PATH."/warrant.txt", 'a')) != FALSE) {
151         /* Unix time | session | nickname | IP | where was | mesg */
152         fwrite($fp, sprintf("%ld|%s|%s|%s|\n", $curtime, $user->name, xcapelt(urldecode($cli_name)), xcapelt(urldecode($cli_email))));
153         fclose($fp);
154       }
155       Warrant::unlock_data($wa_lock);
156       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
157       /* MLANG: "<br>Il nominativo &egrave; stato inoltrato all\'amministratore.<br><br>Nell\'arco di pochi giorni vi verr&agrave;<br><br>notificata l\'avvenuta registrazione." */
158       $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);
159       $user->step_inc();
160       echo "1";
161     }
162     else {
163       /* MLANG: "<b>E\' occorso un errore durante il salvataggio, riprova o contatta l\'amministratore.</b>" */
164       $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);
165     }
166     
167   }
168   else {
169     /* MLANG: "<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>" */
170     $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>");', $dt, NICKSERV);
171   }
172
173   if ($mesg_to_user != "") {
174     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
175     
176     $dt = date("H:i ", $curtime);
177     $user->comm[$user->step % COMM_N] .= $mesg_to_user;
178     $user->step_inc();
179   }
180 }
181 /******************
182  *                *
183  *   STAT: room   *
184  *                *
185  ******************/
186 else if ($user->stat == 'room') {
187   $user->laccwr = time();
188
189   if ($argz[0] == 'help') {
190     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
191     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_help), 0, "torna ai tavoli", 600, 500);
192
193     log_wr($user->comm[$user->step % COMM_N]);
194     $user->step_inc();
195     
196   }
197   else if ($argz[0] == 'about') {
198     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
199     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_about), 0, "torna ai tavoli", 400, 200);
200
201     log_wr($user->comm[$user->step % COMM_N]);
202     $user->step_inc();
203     
204   }
205   else if ($argz[0] == 'roadmap') {
206     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
207     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_roadmap), 0, "torna ai tavoli", 400, 200);
208
209     log_wr($user->comm[$user->step % COMM_N]);
210     $user->step_inc();
211     
212   }
213   else if ($argz[0] == 'whysupport') {
214     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
215     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_whysupport), 0, "torna ai tavoli", 400, 200);
216
217     log_wr($user->comm[$user->step % COMM_N]);
218     $user->step_inc();
219     
220   }
221   else if ($argz[0] == 'chatt') {
222     $room->chatt_send(&$user,$mesg);
223   }
224   /**********************
225    *                    *
226    *   SUBST: standup   *
227    *                    *
228    **********************/
229   else if ($user->subst == 'standup') {
230    
231     if ($argz[0] == 'sitdown') {
232       log_wr("SITDOWN command");
233
234       if ($user->the_end == TRUE) {
235         log_wr("INFO:SKIP:argz == sitdown && the_end == TRUE => ignore request.");
236         Room::unlock_data($sem);
237         exit;
238       }
239
240       // Take parameters
241       $table_idx = $argz[1];
242       $table = &$room->table[$table_idx];
243     
244       $curtime = time();
245
246       if ($G_shutdown || $table->wakeup_time > $curtime || 
247           ($table->auth_only && (($user->flags & USER_FLAG_AUTH) == 0)) ) {
248         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
249
250         $dt = date("H:i ", $curtime);
251         /* MLANG: "<b>Il server sta per essere riavviato, non possono avere inizio nuove partite.</b>", "<b>Il tavolo a cui volevi sederti richiede autentifica.</b>", "<b>Il tavolo si &egrave; appena liberato, ci si potr&agrave; sedere tra %d secondi.</b>" */
252         if ($G_shutdown) {
253           $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);
254         }
255         else if ($table->auth_only && (($user->flags & USER_FLAG_AUTH) == 0)) {
256           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>Il tavolo a cui volevi sederti richiede autentifica.</b>");', $dt, NICKSERV);
257         }
258         else {
259           $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);
260         }
261         $user->step_inc();
262         Room::save_data($room);
263         Room::unlock_data($sem);
264         exit;
265       }
266
267       /* TODO: refact to a function */
268       // if ($user->bantime > $user->laccwr) {
269       require_once("Obj/hardban.phh");
270
271       if (($bantime = Hardbans::check(($user->flags & USER_FLAG_AUTH ? $user->name : FALSE),
272                           $user->ip, $user->sess)) != -1) {
273         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
274         /* MLANG: "<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.", "resta in piedi.", "<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." */
275         if ($user->flags & USER_FLAG_AUTH) {
276           $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);
277         }
278         else {
279           $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);
280         }
281         $user->step_inc();
282         Room::save_data($room);
283         Room::unlock_data($sem);
284         exit;
285       }
286     
287       if ($table->player_n == PLAYERS_N) {
288         log_wr("WARN:FSM: Sitdown unreachable, table full.");
289         Room::unlock_data($sem);
290         exit;
291       } 
292       
293       // set new status
294       $user->subst = "sitdown";
295       $user->table = $table_idx;
296       $user->table_pos = $table->user_add($idx);
297       
298       log_wr("MOP before");
299
300       if ($table->player_n == PLAYERS_N) {
301         log_wr("MOP inall");
302
303         // Start game for this table.
304         log_wr("Start game!");
305         
306         //
307         //  START THE SPAWN HERE!!!!
308         //
309
310         $curtime = time();
311
312         // Create new spawned table
313         $bri_sem = Briskin5::lock_data($table_idx);
314         $table_token = uniqid("");
315         $room->table[$table_idx]->table_token = $table_token;
316         $room->table[$table_idx]->table_start = $curtime;
317         
318         $plist = "$table_token|$user->table|$table->player_n";
319         for ($i = 0 ; $i < $table->player_n ; $i++) {
320           $plist .= '|'.$room->user[$table->player[$i]]->sess;
321         }
322         log_legal($curtime, $user, "STAT:CREATE_GAME", $plist);
323
324         if (($bri =& new Briskin5(&$room, $table_idx, $table_token)) == FALSE)
325           log_wr("bri create: FALSE");
326         else
327           log_wr("bri create: ".serialize($bri));
328         
329         // init table
330         $bri_table =& $bri->table[0];
331         $bri_table->init(&$bri->user);
332         $bri_table->game_init(&$bri->user);
333         //
334         // Init spawned users.
335         //
336         require_once('briskin5/Obj/briskin5.phh');
337         //
338         //  MULTIGAME: here init of selected game instead of hardcabled briskin5 init (look subst status)
339         // 
340         for ($i = 0 ; $i < $table->player_n ; $i++) {
341           $bri_user_cur = &$bri->user[$i];
342           $user_cur = &$room->user[$table->player[$i]];
343           
344           $bri_user_cur->stat_set('table');
345           $bri_user_cur->subst = 'asta';
346           $bri_user_cur->laccwr = $curtime;
347           
348           $bri_user_cur->trans_step = $user_cur->step + 1;
349           $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = "";
350           $bri_user_cur->step_inc();
351           $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = show_table(&$bri,&$bri_user_cur,$bri_user_cur->step+1,TRUE, FALSE);
352           
353           $bri_user_cur->step_inc();
354           
355           log_wr("TRY PRESAVE: ".$bri_user_cur->step." TRANS STEP: ".$bri_user_cur->trans_step);
356           
357           log_wr("Pre if!");
358           
359           //          ARRAY_POP DISABLED
360           //        // CHECK
361           while (array_pop($user_cur->comm) != NULL);
362           
363           $ret = "";
364           $ret .= sprintf('gst.st_loc++; gst.st=%d; createCookie("table_idx", %d, 24*365, cookiepath); createCookie("table_token", "%s", 24*365, cookiepath); createCookie("lang", "%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, $G_lang);
365           
366           $user_cur->comm[$user_cur->step % COMM_N] = $ret;
367           $user_cur->trans_step = $user_cur->step + 1;
368           log_wr("TRANS ATTIVATO");
369           
370           
371           $user_cur->stat_set('table');
372           $user_cur->subst = 'asta';
373           $user_cur->laccwr = $curtime;
374           $user_cur->step_inc();
375         }
376         log_wr("presave bri");
377         Briskin5::save_data($bri);
378         Briskin5::unlock_data($bri_sem);
379         log_wr("postsave bri");
380       }
381       // change room
382       $room->room_sitdown(&$user, $table_idx);
383
384       log_wr("MOP finish");
385
386       
387     }
388     else if ($argz[0] == 'logout') {
389       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
390       $user->comm[$user->step % COMM_N] .= 'postact_logout();';
391       $user->the_end = TRUE;
392       $user->step_inc();
393     }
394   }
395   /**********************
396    *                    *
397    *   SUBST: sitdown   *
398    *                    *
399    **********************/
400   else if ($user->subst == 'sitdown') {
401     if ($argz[0] == 'wakeup') {
402       $room->room_wakeup(&$user);      
403     }
404     else if ($argz[0] == 'logout') {
405       $room->room_wakeup(&$user);      
406       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
407       $user->comm[$user->step % COMM_N] .= 'postact_logout();';
408       $user->the_end = TRUE;
409       $user->step_inc();
410     }
411   }
412 }
413 log_wr("before save data");
414 Room::save_data($room);
415
416 Room::unlock_data($sem);
417 exit;
418 ?>