return bug fixed, sidebanner, bg updated
[brisk.git] / web / index_wr.php
1 <?php
2 /*
3  *  brisk - index_wr.php
4  *
5  *  Copyright (C) 2006-2009 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
31 // Use of proxies isn't allowed.
32 // if (is_proxy()) {
33 //   sleep(5);
34 //   exit;
35 // }
36 log_load("index_wr.php");
37
38 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
39   echo "Debugging time!";
40   exit;
41 }
42
43 /*
44  *  MAIN
45  */
46
47 /* if the IP is banned, exit without do nothing */
48 if (array_search($_SERVER['REMOTE_ADDR'], $G_black_list) !== FALSE) {
49   sleep(5);
50   exit;
51 }
52
53 $is_spawn = FALSE;
54
55 log_wr('COMM: '.xcapemesg($mesg));
56
57 $sem = Room::lock_data();
58 if (($room = &Room::load_data()) == FALSE) {
59   echo "Load data error";
60   log_wr("Load data error");
61   Room::unlock_data($sem);
62   exit;
63 }
64 if (($user = &$room->get_user($sess, &$idx)) == FALSE) {
65   Room::unlock_data($sem);
66   $argz = explode('|', xcapemesg($mesg));
67
68   if ($argz[0] == 'getchallenge') {
69     GLOBAL $cli_name;
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(urldecode($cli_name))) != 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     /* MLANG: "torna ai tavoli" */ 
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('|', xcapemesg($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 }
140 else if ($argz[0] == 'warranty') {
141   GLOBAL $cli_name, $cli_email;
142
143   $curtime = time();
144   $mesg_to_user = "";
145
146   log_wr("INFO:SKIP:argz == warranty name: [".$cli_name."] AUTH: ".($user->flags & USER_FLAG_AUTH));
147   if ($user->flags & USER_FLAG_AUTH) {
148     if (($wa_lock = Warrant::lock_data()) != FALSE) {
149       if (($fp = @fopen(LEGAL_PATH."/warrant.txt", 'a')) != FALSE) {
150         /* Unix time | session | nickname | IP | where was | mesg */
151         fwrite($fp, sprintf("%ld|%s|%s|%s|\n", $curtime, $user->name, xcapelt(urldecode($cli_name)), xcapelt(urldecode($cli_email))));
152         fclose($fp);
153       }
154       Warrant::unlock_data($wa_lock);
155       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
156       /* 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." */
157       $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);
158       $user->step_inc();
159       echo "1";
160     }
161     else {
162       /* MLANG: "<b>E\' occorso un errore durante il salvataggio, riprova o contatta l\'amministratore.</b>" */
163       $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);
164     }
165     
166   }
167   else {
168     /* MLANG: "<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>" */
169     $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>");', $dt, NICKSERV);
170   }
171
172   if ($mesg_to_user != "") {
173     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
174     
175     $dt = date("H:i ", $curtime);
176     $user->comm[$user->step % COMM_N] .= $mesg_to_user;
177     $user->step_inc();
178   }
179 }
180 else if ($argz[0] == 'mesgtoadm') {
181   GLOBAL $cli_subj, $cli_mesg;
182
183   $curtime = time();
184   $mesg_to_user = "";
185
186   log_wr("INFO:SKIP:argz == mesgtoadm name: [".$cli_name."] AUTH: ".($user->flags & USER_FLAG_AUTH));
187   if ($user->flags & USER_FLAG_AUTH) {
188     if (($wa_lock = Warrant::lock_data()) != FALSE) {
189       if (($fp = @fopen(LEGAL_PATH."/messages.txt", 'a')) != FALSE) {
190         /* Unix time | session | nickname | IP | where was | mesg */
191         fwrite($fp, sprintf("%ld|%s|%s|%s\n", $curtime, $user->name, 
192                             xcapelt(urldecode($cli_subj)), xcapelt(urldecode($cli_mesg))));
193         fclose($fp);
194       }
195       Warrant::unlock_data($wa_lock);
196       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
197       /* MLANG: "" */
198       $user->comm[$user->step % COMM_N] .=  show_notify("<br><br>Il messaggio &egrave; stato inoltrato all\'amministratore.", 0, "chiudi", 400, 110);
199       $user->step_inc();
200       echo "1";
201     }
202     else {
203       /* MLANG: "<b>E\' occorso un errore durante il salvataggio, riprova o contatta l\'amministratore.</b>" */
204       $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"<b>E\' occorso un errore durante il salvataggio, riprova o contatta per mail l\'amministratore.</b>");', $dt, NICKSERV);
205     }
206     
207   }
208   else {
209     /* MLANG: "<b>Per autenticare qualcuno devi a tua volta essere autenticato.</b>" */
210     $mesg_to_user = sprintf('chatt_sub("%s", [2, "%s"],"<b>Per mandare messaggi all\'amministratore devi essere autenticato.</b>");', $dt, NICKSERV);
211   }
212
213   if ($mesg_to_user != "") {
214     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
215     
216     $dt = date("H:i ", $curtime);
217     $user->comm[$user->step % COMM_N] .= $mesg_to_user;
218     $user->step_inc();
219   }
220 }
221
222
223 /******************
224  *                *
225  *   STAT: room   *
226  *                *
227  ******************/
228 else if ($user->stat == 'room') {
229   $user->laccwr = time();
230
231   if ($argz[0] == 'help') {
232     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
233     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_help), 0, "torna ai tavoli", 600, 500);
234
235     log_wr($user->comm[$user->step % COMM_N]);
236     $user->step_inc();
237     
238   }
239   else if ($argz[0] == 'about') {
240     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
241     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_about), 0, "torna ai tavoli", 400, 200);
242
243     log_wr($user->comm[$user->step % COMM_N]);
244     $user->step_inc();
245     
246   }
247   else if ($argz[0] == 'roadmap') {
248     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
249     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_roadmap), 0, "torna ai tavoli", 400, 200);
250
251     log_wr($user->comm[$user->step % COMM_N]);
252     $user->step_inc();
253     
254   }
255   else if ($argz[0] == 'whysupport') {
256     $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
257     $user->comm[$user->step % COMM_N] .=  show_notify(str_replace("\n", " ", $G_room_whysupport), 0, "torna ai tavoli", 400, 200);
258
259     log_wr($user->comm[$user->step % COMM_N]);
260     $user->step_inc();
261     
262   }
263   else if ($argz[0] == 'chatt') {
264     $room->chatt_send(&$user, xcapemesg($mesg));
265   }
266   /**********************
267    *                    *
268    *   SUBST: standup   *
269    *                    *
270    **********************/
271   else if ($user->subst == 'standup') {
272    
273     if ($argz[0] == 'sitdown') {
274       log_wr("SITDOWN command");
275
276       if ($user->the_end == TRUE) {
277         log_wr("INFO:SKIP:argz == sitdown && the_end == TRUE => ignore request.");
278         Room::unlock_data($sem);
279         exit;
280       }
281
282       // Take parameters
283       $table_idx = $argz[1];
284       $table = &$room->table[$table_idx];
285     
286       $curtime = time();
287
288       if ($G_shutdown || $table->wakeup_time > $curtime || 
289           ($table->auth_only && (($user->flags & USER_FLAG_AUTH) == 0)) ) {
290         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
291
292         $dt = date("H:i ", $curtime);
293         /* 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>" */
294         if ($G_shutdown) {
295           $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);
296         }
297         else if ($table->auth_only && (($user->flags & USER_FLAG_AUTH) == 0)) {
298           $user->comm[$user->step % COMM_N] .= sprintf('chatt_sub("%s", [2, "%s"],"<b>Il tavolo a cui volevi sederti richiede autentifica.</b>");', $dt, NICKSERV);
299         }
300         else {
301           $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);
302         }
303         $user->step_inc();
304         Room::save_data($room);
305         Room::unlock_data($sem);
306         exit;
307       }
308
309       /* TODO: refact to a function */
310       // if ($user->bantime > $user->laccwr) {
311       require_once("Obj/hardban.phh");
312
313       if (($bantime = Hardbans::check(($user->flags & USER_FLAG_AUTH ? $user->name : FALSE),
314                           $user->ip, $user->sess)) != -1) {
315         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
316         /* 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." */
317         if ($user->flags & USER_FLAG_AUTH) {
318           $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);
319         }
320         else {
321           $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);
322         }
323         $user->step_inc();
324         Room::save_data($room);
325         Room::unlock_data($sem);
326         exit;
327       }
328     
329       if ($table->player_n == PLAYERS_N) {
330         log_wr("WARN:FSM: Sitdown unreachable, table full.");
331         Room::unlock_data($sem);
332         exit;
333       } 
334       
335       // set new status
336       $user->subst = "sitdown";
337       $user->table = $table_idx;
338       $user->table_pos = $table->user_add($idx);
339       
340       log_wr("MOP before");
341
342       if ($table->player_n == PLAYERS_N) {
343         require_once("briskin5/Obj/briskin5.phh");
344         log_wr("MOP inall");
345
346         // Start game for this table.
347         log_wr("Start game!");
348         
349         //
350         //  START THE SPAWN HERE!!!!
351         //
352
353         $curtime = time();
354
355         // Create new spawned table
356         $bri_sem = Briskin5::lock_data($table_idx);
357         $table_token = uniqid("");
358         $room->table[$table_idx]->table_token = $table_token;
359         $room->table[$table_idx]->table_start = $curtime;
360         
361         $plist = "$table_token|$user->table|$table->player_n";
362         for ($i = 0 ; $i < $table->player_n ; $i++) {
363           $plist .= '|'.$room->user[$table->player[$i]]->sess;
364         }
365         log_legal($curtime, $user, "STAT:CREATE_GAME", $plist);
366
367         if (($bri =& new Briskin5(&$room, $table_idx, $table_token)) == FALSE)
368           log_wr("bri create: FALSE");
369         else
370           log_wr("bri create: ".serialize($bri));
371         
372         // init table
373         $bri_table =& $bri->table[0];
374         $bri_table->init(&$bri->user);
375         $bri_table->game_init(&$bri->user);
376         //
377         // Init spawned users.
378         //
379         require_once('briskin5/Obj/briskin5.phh');
380         //
381         //  MULTIGAME: here init of selected game instead of hardcabled briskin5 init (look subst status)
382         // 
383         for ($i = 0 ; $i < $table->player_n ; $i++) {
384           $bri_user_cur = &$bri->user[$i];
385           $user_cur = &$room->user[$table->player[$i]];
386           
387           $bri_user_cur->stat_set('table');
388           $bri_user_cur->subst = 'asta';
389           $bri_user_cur->laccwr = $curtime;
390           
391           $bri_user_cur->trans_step = $user_cur->step + 1;
392           $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = "";
393           $bri_user_cur->step_inc();
394           $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = show_table(&$bri,&$bri_user_cur,$bri_user_cur->step+1,TRUE, FALSE);
395           
396           $bri_user_cur->step_inc();
397           
398           log_wr("TRY PRESAVE: ".$bri_user_cur->step." TRANS STEP: ".$bri_user_cur->trans_step);
399           
400           log_wr("Pre if!");
401           
402           //          ARRAY_POP DISABLED
403           //        // CHECK
404           while (array_pop($user_cur->comm) != NULL);
405           
406           $ret = "";
407           $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);
408           
409           $user_cur->comm[$user_cur->step % COMM_N] = $ret;
410           $user_cur->trans_step = $user_cur->step + 1;
411           log_wr("TRANS ATTIVATO");
412           
413           
414           $user_cur->stat_set('table');
415           $user_cur->subst = 'asta';
416           $user_cur->laccwr = $curtime;
417           $user_cur->step_inc();
418         }
419         log_wr("presave bri");
420         Briskin5::save_data($bri);
421         Briskin5::unlock_data($bri_sem);
422         log_wr("postsave bri");
423       }
424       // change room
425       $room->room_sitdown(&$user, $table_idx);
426
427       log_wr("MOP finish");
428
429       
430     }
431     else if ($argz[0] == 'logout') {
432       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
433       $user->comm[$user->step % COMM_N] .= 'postact_logout();';
434       $user->the_end = TRUE;
435       $user->step_inc();
436     }
437   }
438   /**********************
439    *                    *
440    *   SUBST: sitdown   *
441    *                    *
442    **********************/
443   else if ($user->subst == 'sitdown') {
444     if ($argz[0] == 'wakeup') {
445       $room->room_wakeup(&$user);      
446     }
447     else if ($argz[0] == 'logout') {
448       $room->room_wakeup(&$user);      
449       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
450       $user->comm[$user->step % COMM_N] .= 'postact_logout();';
451       $user->the_end = TRUE;
452       $user->step_inc();
453     }
454   }
455 }
456 log_wr("before save data");
457 Room::save_data($room);
458
459 Room::unlock_data($sem);
460 exit;
461 ?>