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