Location to briskin5
[brisk.git] / web / index.php
1 <?php
2 /*
3  *  brisk - index.php
4  *
5  *  Copyright (C) 2006-2007 matteo.nastasi@milug.org
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABLILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details. You should have received a
16  * copy of the GNU General Public License along with this program; if
17  * not, write to the Free Software Foundation, Inc, 59 Temple Place -
18  * Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * $Id$
21  *
22  */
23
24 require_once("Obj/brisk.phh");
25 require_once("briskin5/Obj/briskin5.phh");
26 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
27   echo "Debugging time!";
28   exit;
29 }
30
31 log_load((isset($sess) ? $sess : "XXX"), "LOAD: index.php");
32
33 function main()
34 {
35   GLOBAL $sess, $name, $table_idx, $BRISK_SHOWHTML, $BRISK_DEBUG, $_SERVER;
36   
37   $body = "";
38   $tables = "";
39   $standup = "";
40   $ACTION = "login";
41   
42   if (isset($BRISK_SHOWHTML) == FALSE) {
43     $is_table = FALSE;
44     $sem = Room::lock_data();
45     log_load($sess, "lock Room");
46     $room = &Room::load_data();
47     
48     /* Actions */
49     if (validate_sess($sess)) {
50       // FIXME uncomment $room->garbage_manager(TRUE);
51       if (($user = &$room->get_user($sess, &$idx)) != FALSE) {
52         if ($user->stat == "table") {
53           $change_page = TRUE;
54           log_load($sess, "resync from index.php");
55
56           if (isset($table_idx)) {
57             $table_idx = (int)$table_idx;
58             log_load($sess, "SET TABLE_IDX <yy".$table_idx.">".TABLES_N);
59             if ($table_idx >= 0 && $table_idx < TABLES_N) {
60               log_load($sess, "SET TABLE_IDX GOOD VALUE");
61               $bri_sem = Briskin5::lock_data($table_idx);
62               $bri = &Briskin5::load_data($table_idx);
63
64               if (($bri_user = &$bri->get_user($sess, &$bri_idx)) != FALSE) {
65                 if ($bri_user->subst == "shutdowned" || $bri_user->subst == "shutdowner") {
66                   // QUI WAKEUP
67                   $table     = &$room->table[$user->table];
68                   $bri_table = &$bri->table[0];
69
70                   for ($i = 0 ; $i < $bri_table->player_n ; $i++) {
71                     $room->user[$table->player[$i]]->subst = $bri->user[$i]->subst;
72                     $room->user[$table->player[$i]]->step = $bri->user[$i]->step;
73                     $room->user[$table->player[$i]]->trans_step = $bri->user[$i]->step+1;
74                     log_load($sess, "from table bri subst[".$i."]: ".$bri->user[$i]->subst);
75                     log_load($sess, "from table roo subst[".$i."]: ".$room->user[$table->player[$i]]->subst);
76                   }
77
78                   $room->room_join_wakeup(&$user);
79
80                   if (Room::save_data(&$room) == FALSE) {
81                     echo "ERRORE SALVATAGGIO\n";
82                     exit;
83                   }
84
85                   $change_page = FALSE;
86                 }
87                 log_load($sess, "from table subst: ".$bri_user->subst);
88               }
89               Briskin5::unlock_data($bri_sem);
90             }
91           }
92           log_load($sess, "unlock Room");
93           if ($change_page) {
94             Room::unlock_data($sem);
95             header ("Location: briskin5/briskin5.php");
96             exit;
97           }
98         }
99         $ACTION = "room";
100       }
101     }
102     
103     if ($ACTION == "login" && isset($name)) {
104       // FIXME uncomment $room->garbage_manager(TRUE);
105       /* try login */
106       if (($user = &$room->add_user(&$sess, &$idx, $name, $_SERVER['REMOTE_ADDR'])) != FALSE) {
107         $ACTION = "room";
108         
109         // setcookie ("sess", "", time() + 180);      
110         $room->standup_update(&$user);
111         
112         if (Room::save_data(&$room) == FALSE) {
113           echo "ERRORE SALVATAGGIO\n";
114           exit;
115         }
116       }
117       else {
118         /* Login Rendering */
119         if ($idx == -2)
120           $body .= '<div class="urgmsg"><b>Il nickname deve contenere almeno una lettera o una cifra.</b></div>';
121         else if ($idx == -1) 
122           $body .= '<div class="urgmsg"><b>Spiacenti, non ci sono pi&ugrave; posti liberi. Riprova pi&ugrave; tardi.</b></div>';
123         else
124           $body .= '<div class="urgmsg"><b>Il tuo nickname &egrave; gi&agrave; in uso.</b></div>';
125       }
126     }
127     Room::unlock_data($sem);
128   }
129   /* Rendering. */
130
131   if ($BRISK_SHOWHTML == "debugtable") {
132     $ACTION = "room";
133   }
134   else if ($BRISK_SHOWHTML == "debuglogin") {
135     $ACTION = "login";
136   }
137
138   if ($ACTION == "room") {
139     $tables .= '<div class="room_tab">';
140     $tables .= '<table class="room_tab" align="center">';
141     for ($i = 0 ; $i < TABLES_N ; $i++) {
142       if ($i % 4 == 0)
143         $tables .= '<tr>';
144       $tables .= '<td>';
145       $tables .= '<div class="room_div"><div class="room_tit"><b>Tavolo '.$i.'</b></div>';
146       $tables .= sprintf('<div class="proxhr" id="table%d"></div>', $i);
147       $tables .= sprintf('<div class="table_act" id="table_act%d"></div>', $i);
148       $tables .= '</div>';
149       $tables .= '</td>'."\n";
150       if ($i % 4 == 3)
151         $tables .= '</tr>';
152     }
153     $tables .= '</table></div>';
154
155
156     $standup .= '<table class="room_standup" align="center"><tr><td>';
157     $standup .= '<div class="room_ex_standup">';
158     $standup .= '<div class="room_tit"><b>Giocatori in piedi</b></div>';
159     
160     $standup .= sprintf('<div id="standup" class="room_standup"></div>');
161     $standup .= '<div id="esco" class="esco"></div>';
162     $standup .= '</div></td></tr></table>';
163     
164     // $tables .= '</td></tr></table>';
165
166     /*
167     $tables .= '</td></tr><tr><td>';
168     $tables .= '<table class="room_tab" align="center">';
169     $tables .= '<tr><td>';
170     $tables .= '<div class="room_ex_standup">';
171     $tables .= '<b>Giocatori in piedi</b>';
172     
173     $tables .= sprintf('<div id="standup" class="room_standup"></div>');
174     $tables .= '<div id="esco" class="esco"></div>';
175     */
176     // $tables .= '</td></tr></table>';
177   }
178
179   $altout_propag = array( array ( 'url' => 'http://www.alternativeoutput.it',
180                                   'content' => '<img class="nobo" src="img/altout80x15.png">' ),
181                           array ( 'url' => 'http://virtualsky.alternativeoutput.it',
182                                   'content' => '<img class="nobo" src="img/virtualsky80x15a.gif">' )
183                           );
184   
185   // seed with microseconds since last "whole" second
186   srand ((double) microtime() * 1000000);
187   // $randval = rand(0,count($altout_propag)-1);
188   $randval = 1;
189   $altout_carousel = sprintf('<a target="_blank" href="%s">%s</a>',
190                              $altout_propag[$randval]['url'],
191                              $altout_propag[$randval]['content']);
192                          
193
194   $brisk_donate = file_get_contents(FTOK_PATH."/brisk_donate.txt");
195   if ($brisk_donate == FALSE)
196     $brisk_donate = "";
197
198 $brisk_header_form = '<div class="container">
199 <!-- =========== header ===========  -->
200 <div id="header" class="header">
201 <table width="100%%" border="0" cols="3"><tr>
202 <td align="left"><div style="padding-left: 8px;">
203
204
205
206 <script type="text/javascript"><!--
207 google_ad_client = "pub-5246925322544303";
208 google_ad_width = 234;
209 google_ad_height = 60;
210 google_ad_format = "234x60_as";
211 google_ad_type = "text_image";
212 google_ad_channel = "";
213 google_color_border = "808080";
214 google_color_bg = "f6f6f6";
215 google_color_link = "ffae00";
216 google_color_text = "404040";
217 google_color_url = "000000";
218 //-->
219 </script>
220 <script type="text/javascript"
221   src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
222 </script>
223
224
225
226 </div></td>
227 <td align="center">
228 <!-- <table><tr><td>  -->
229 <div>
230     <img class="nobo" src="img/brisk_logo64.png">
231     briscola chiamata in salsa ajax<br>
232     </div>
233 <!-- </td><td><div style="align: center; text-align:center; background-color: #f8f8f8; padding: 2px; border: 1px solid #ffae00;"><a href="http://www.linuxday.it"><img class="nobo" src="img/ld66.png"></a> 27/10/2007<br>OGGI! 
234     </td></tr></table>-->
235 </td>
236 <td align="right"><div style="padding-right: 8px;">
237
238
239
240 <script type="text/javascript"><!--
241 google_ad_client = "pub-5246925322544303";
242 google_ad_width = 234;
243 google_ad_height = 60;
244 google_ad_format = "234x60_as";
245 google_ad_type = "text_image";
246 google_ad_channel = "";
247 google_color_border = "808080";
248 google_color_bg = "f6f6f6";
249 google_color_link = "ffae00";
250 google_color_text = "404040";
251 google_color_url = "000000";
252 //-->
253 </script>
254 <script type="text/javascript"
255   src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
256 </script>
257
258
259
260 </div></td>
261 </td></table>
262 </div>';
263
264 $brisk_vertical_menu = '
265 <!--  =========== vertical menu ===========  -->
266 <div class="topmenu">
267 <!-- <a target="_blank" href="/briskhome.php"></a> -->
268 <img class="nobo" src="img/brisk_start.png" onmouseover="$(\'webstart\').style.visibility = \'visible\';">
269 <div class="webstart" id="webstart" onmouseover="this.style.visibility = \'visible\';" onmouseout="this.style.visibility = \'hidden\';">
270 <a target="_blank" href="http://www.alternativeoutput.it/briskhome.php" title="homepage del progetto">homepage</a><br>
271 <a target="_blank" href="http://www.alternativeoutput.it/briskhome.php#cose" title="di cosa si tratta">cos\'&egrave;</a><br>
272 <a target="_blank" href="http://it.wikipedia.org/wiki/Briscola#Gioco_a_5" title="come si gioca">regole</a><br>
273 <a target="_blank" href="http://www.alternativeoutput.it/briskhome.php#shots" title="screenshots dell\'applicazione">screenshoots</a><br>
274 <a target="_blank" href="http://www.alternativeoutput.it/briskhome.php#comp" title="compatibilit&agrave; con i browser">compatibilit&agrave;</a><br>
275 <a target="_blank" href="http://www.alternativeoutput.it/briskhome.php#sources" title="sorgenti dell\'applicazione">sorgenti</a><br>
276 <a target="_blank" href="http://www.alternativeoutput.it/briskhome.php#mailing" title="come iscriversi alla mailing list">mailing&nbsp;list</a><br>
277
278 <a target="_blank" href="http://www.alternativeoutput.it/briskhome.php#prop" title="come fare pubblicit&agrave; a brisk!">propaganda</a><br>
279 <a href="#" title="credits" onclick="act_about();">about</a><br>
280 <a href="mailto:brisk@alternativeoutput.it" title="contatti">contatti</a><br>
281
282 </div>
283 <br><br><br>
284 sponsored by:<br><br>'.$altout_carousel.'<br>
285 <a target="_blank" href="http://www.dynamica.it"><img class="nobo" src="img/dynamica.png"></a><br><br>
286 supported by:<br><br>
287 <a target="_blank" href="http://www.briscolachiamata.it"><img class="nobo" src="img/brichi.png"></a><br><br>
288 <div id="proflashext" class="proflashext"><div id="proflash" class="proflash">
289 </div><br><br></div>
290 %s
291 %s
292 </div>';
293     
294   /* Templates. */
295   if ($ACTION == 'login') {
296 ?>
297 <html>
298 <head>
299 <title>Brisk</title>
300 <link rel="shortcut icon" href="img/brisk_ico.png">
301 <script type="text/javascript" src="dnd.js"></script>
302 <script type="text/javascript" src="dom-drag.js"></script>
303 <script type="text/javascript" src="commons.js"></script> 
304 <script type="text/javascript" src="xhr.js"></script>
305 <script type="text/javascript" src="preload_img.js"></script>
306 <script type="text/javascript" src="AC_OETags.js"></script>
307 <link rel="stylesheet" type="text/css" href="brisk.css">
308 <link rel="stylesheet" type="text/css" href="room.css">
309 </head>
310 <body>
311 <SCRIPT type="text/javascript">
312    var g_withflash = false;
313    var g_is_spawn = 0; 
314    window.onload = function() {
315      g_withflash = DetectFlashVer(6,0,0);
316      if (g_withflash == false) {
317        $("proflash").innerHTML = 'Audio con Flash.<br><a href="http://www.macromedia.com/"><img class="nobo" style="padding: 4px; width:73; height: 19;" src="img/download_now_flash.gif"></a>';
318      }
319      else
320        $("proflashext").innerHTML = "";
321      $("nameid").focus();
322    }
323 </SCRIPT>
324 <?php
325     printf($brisk_header_form);
326     printf("<table class=\"floaty\"><tr><td class=\"floatyleft\">\n");
327     printf($brisk_vertical_menu, '', '');
328     printf("</td><td>");
329 ?> 
330
331 <!--  =========== tables ===========  -->
332 <?php echo "$body"; ?>
333 <br>
334 <div style="text-align: center;">
335    <br><br><br>
336 Digita il tuo nickname per accedere ai tavoli della briscola.<br><br>
337 <form method="post" action="">
338 <input id="nameid" name="name" type="text" size="24" maxlength="12" value="">
339 <input id="sub"    value="entra" type="submit" class="button">
340 </form>
341 </div></td></tr></table>
342 <br><br><br><br>
343
344 <div id="imgct"></div>
345 <div id="logz"></div>
346 <div id="sandbox"></div>
347 <div id="sandbox2"></div>
348 <div id="response"></div>
349 <div id="xhrstart"></div>
350 <pre>
351 <div id="xhrlog"></div>
352 </pre>
353 <div id="xhrdeltalog"></div>
354 </body>
355 </html>
356 <?php
357   }
358   else if ($ACTION == 'room') {
359   ?>
360 <html>
361 <head>
362 <title>Brisk</title>
363 <link rel="shortcut icon" href="img/brisk_ico.png">
364 <script type="text/javascript" src="dnd.js"></script>
365 <script type="text/javascript" src="dom-drag.js"></script>
366 <script type="text/javascript" src="commons.js"></script> 
367 <script type="text/javascript" src="xhr.js"></script>
368 <script type="text/javascript" src="preload_img.js"></script>
369 <script type="text/javascript" src="AC_OETags.js"></script>
370 <link rel="stylesheet" type="text/css" href="brisk.css">
371 <link rel="stylesheet" type="text/css" href="room.css">
372 </head>
373 <body>
374 <SCRIPT type="text/javascript">
375    var sess;
376    var stat = "";
377    var subst = "";
378    var gst  = new globst();
379    var g_is_spawn = 0; 
380
381    var g_withflash = false;
382    var g_imgct= 0;
383    var g_imgtot = g_preload_img_arr.length;
384    var myfrom = "index_php";
385    window.onload = function() {
386 <?php
387 if ($BRISK_SHOWHTML == "debugtable") {
388 ?>
389      room_checkspace(12, <?php echo TABLES_N; ?>, 50);
390 <?php
391 }
392 else {
393 ?>
394      // alert("INDEX START");
395      xhr_rd = createXMLHttpRequest();
396      sess = "<?php echo "$sess"; ?>";
397
398      window.onunload = onunload_cb;
399      g_withflash = DetectFlashVer(6,0,0);
400      if (g_withflash == false) {
401        $("proflash").innerHTML = 'Audio con Flash.<br><a href="http://www.macromedia.com/"><img class="nobo" style="padding: 4px; width:73; height: 19;" src="img/download_now_flash.gif"></a>';
402      }
403      else
404        $("proflashext").innerHTML = "";
405      setTimeout(xhr_rd_poll, 0, sess); 
406      // alert("ARR LENGTH "+g_preload_img_arr.length);
407      setTimeout(preload_images, 0, g_preload_img_arr, g_imgct); 
408      $("txt_in").focus();
409 <?php
410 }
411 ?>
412    }
413
414 </SCRIPT>
415 <?php
416    printf($brisk_header_form);
417    printf("<table class=\"floaty\"><tr><td class=\"floatyleft\">\n");
418    printf($brisk_vertical_menu, '<input type="button" class="button" name="xhelp"  value="Help." onclick="act_help();"><br><!-- <br><input type="button" class="button" name="xabout"  value="About." onclick="act_about();">--><br><br><br>',
419            $brisk_donate);
420    printf("</td><td>");
421 ?> 
422 <!--  =========== tables ===========  -->
423 <input name="sess" type="hidden" value="<?php echo "$user->sess"; ?>">
424 <table class="macro"><tr><td>
425 <?php echo "$tables"; ?>
426 </td></tr><tr><td>
427     <?php echo "$standup"; ?>
428 </td></tr></table>
429 </td></tr></table>
430
431 <!--  =========== bottom ===========  -->
432     <div id="bottom" class="bottom" style="/*  background-color: green; */">
433 <b>Chat</b><br>
434 <div id="txt" class="chatt">
435 </div>
436     <table align=center style="width: 98%; margin: auto;"><tr><td style="width:1%; text-align: right;">
437     <div id="myname"></div>
438     </td><td>
439     <input id="txt_in" type="text" style="width: 100%;" onkeypress="chatt_checksend(this,event);">
440     </td></tr></table>
441 </div>
442 <div id="heartbit"></div>
443 <div id="sandbox"></div>
444 <div id="imgct"></div>
445 <div id="logz"></div>
446 <div id="sandbox2"></div>
447 <div id="response"></div>
448 <div id="remark"></div>
449 <div id="xhrstart"></div>
450 <div id="xhrlog"></div>
451 <div id="xhrdeltalog"></div>
452 </div>
453 </body>
454 </html>
455 <?php
456    }
457 }
458
459 main();
460
461 ?>