rendering for auth names
[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         $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
226         $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);
227         
228         $user->step_inc();
229         Room::save_data($room);
230         Room::unlock_data($sem);
231         exit;
232       }
233     
234       if ($table->player_n == PLAYERS_N) {
235         log_wr("WARN:FSM: Sitdown unreachable, table full.");
236         Room::unlock_data($sem);
237         exit;
238       } 
239       
240       // set new status
241       $user->subst = "sitdown";
242       $user->table = $table_idx;
243       $user->table_pos = $table->user_add($idx);
244       
245       log_wr("MOP before");
246
247       if ($table->player_n == PLAYERS_N) {
248         log_wr("MOP inall");
249
250         // Start game for this table.
251         log_wr("Start game!");
252         
253         //
254         //  START THE SPAWN HERE!!!!
255         //
256
257         $curtime = time();
258
259         // Create new spawned table
260         $bri_sem = Briskin5::lock_data($table_idx);
261         $table_token = uniqid("");
262         $room->table[$table_idx]->table_token = $table_token;
263         $room->table[$table_idx]->table_start = $curtime;
264         
265         $plist = "$table_token|$user->table|$table->player_n";
266         for ($i = 0 ; $i < $table->player_n ; $i++) {
267           $plist .= '|'.$room->user[$table->player[$i]]->sess;
268         }
269         log_legal($curtime, $user->sess, $user->name, "STAT:CREATE_GAME", $plist);
270
271         if (($bri =& new Briskin5(&$room, $table_idx, $table_token)) == FALSE)
272           log_wr("bri create: FALSE");
273         else
274           log_wr("bri create: ".serialize($bri));
275         
276         // init table
277         $bri_table =& $bri->table[0];
278         $bri_table->init(&$bri->user);
279         $bri_table->game_init(&$bri->user);
280         //
281         // Init spawned users.
282         //
283         for ($i = 0 ; $i < $table->player_n ; $i++) {
284           $bri_user_cur = &$bri->user[$i];
285           $user_cur = &$room->user[$table->player[$i]];
286           
287           $bri_user_cur->stat_set('table');
288           $bri_user_cur->subst = 'asta';
289           $bri_user_cur->laccwr = $curtime;
290           
291           $bri_user_cur->trans_step = $user_cur->step + 1;
292           $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = "";
293           $bri_user_cur->step_inc();
294           $bri_user_cur->comm[$bri_user_cur->step % COMM_N] = show_table(&$bri,&$bri_user_cur,$bri_user_cur->step+1,TRUE, FALSE);
295           
296           $bri_user_cur->step_inc();
297           
298           log_wr("TRY PRESAVE: ".$bri_user_cur->step." TRANS STEP: ".$bri_user_cur->trans_step);
299           
300           log_wr("Pre if!");
301           
302           //          ARRAY_POP DISABLED
303           //        // CHECK
304           while (array_pop($user_cur->comm) != NULL);
305           
306           $ret = "";
307           $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);
308           
309           $user_cur->comm[$user_cur->step % COMM_N] = $ret;
310           $user_cur->trans_step = $user_cur->step + 1;
311           log_wr("TRANS ATTIVATO");
312           
313           
314           $user_cur->stat_set('table');
315           $user_cur->subst = 'asta';
316           $user_cur->laccwr = $curtime;
317           $user_cur->step_inc();
318         }
319         log_wr("presave bri");
320         Briskin5::save_data($bri);
321         Briskin5::unlock_data($bri_sem);
322         log_wr("postsave bri");
323       }
324       // change room
325       $room->room_sitdown(&$user, $table_idx);
326
327       log_wr("MOP finish");
328
329       
330     }
331     else if ($argz[0] == 'logout') {
332       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
333       $user->comm[$user->step % COMM_N] .= 'postact_logout();';
334       $user->the_end = TRUE;
335       $user->step_inc();
336     }
337   }
338   /**********************
339    *                    *
340    *   SUBST: sitdown   *
341    *                    *
342    **********************/
343   else if ($user->subst == 'sitdown') {
344     if ($argz[0] == 'wakeup') {
345       $room->room_wakeup(&$user);      
346     }
347     else if ($argz[0] == 'logout') {
348       $room->room_wakeup(&$user);      
349       $user->comm[$user->step % COMM_N] = "gst.st = ".($user->step+1)."; ";
350       $user->comm[$user->step % COMM_N] .= 'postact_logout();';
351       $user->the_end = TRUE;
352       $user->step_inc();
353     }
354   }
355 }
356 log_wr("before save data");
357 Room::save_data($room);
358
359 Room::unlock_data($sem);
360 exit;
361 ?>