2cfa99e735de099e5ddf0330d2d21d3cc4a8ab76
[brisk.git] / web / index.php
1 <?php
2 /*
3  *  brisk - index.php
4  *
5  *  Copyright (C) 2006 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("brisk.phh");
25 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
26   echo "Debugging time!";
27   exit;
28 }
29
30 log_load($sess, "LOAD: index.php");
31
32 function main()
33 {
34   GLOBAL $sess, $name, $BRISK_DEBUG;
35   
36   $body = "";
37   $ACTION = "login";
38   
39   if (isset($BRISK_DEBUG) == FALSE) {
40     $is_table = false;
41     $sem = lock_data();
42     $bri = &load_data();
43     
44     /* Actions */
45     if (isset($sess)) {
46       $bri->garbage_manager(TRUE);
47       if (($user = &get_user(&$bri, $sess, &$idx)) != FALSE) {
48         if ($user->stat == "table") {
49           header ("Location: table.php");
50           unlock_data($sem);
51           exit;
52         }
53         $ACTION = "table";
54       }
55       else {
56         setcookie ("sess", "", time() - 3600);
57       }
58     }
59     else if (isset($name)) {
60       $bri->garbage_manager(TRUE);
61       /* try login */
62       $name = substr($name, 0, 12);
63       if (($user = &add_user(&$bri, &$sess, &$idx, $name)) != FALSE) {
64         $ACTION = "table";
65         
66         setcookie ("sess", "", time() + 180);      
67         standup_update(&$bri,&$user);
68         
69         if (save_data(&$bri) == FALSE) {
70           echo "ERRORE SALVATAGGIO\n";
71           exit;
72         }
73       }
74       else {
75         /* Login Rendering */
76         if ($idx == -1) 
77           $body .= '<div class="urgmsg"><b>Spiacenti, non ci sono pi&ugrave; posti liberi. Riprova pi&ugrave; tardi.</b></div>';
78         else
79           $body .= '<div class="urgmsg"><b>Il tuo nickname &egrave; gi&agrave; in uso.</b></div>';
80       }
81     }
82     unlock_data($sem);
83   }
84   /* Rendering. */
85
86   if ($BRISK_DEBUG == "debugtable") {
87     $ACTION = "table";
88   }
89   else if ($BRISK_DEBUG == "debuglogin") {
90     $ACTION = "login";
91   }
92
93   if ($ACTION == "table") {
94     $tables .= '<table class="room_tab" align="center">';
95     for ($i = 0 ; $i < TABLES_N ; $i++) {
96       if ($i % 4 == 0)
97         $tables .= '<tr>';
98       $tables .= '<td valign="top" align="center" class="room_td"><div class="room_div"><b>Tavolo '.$i.'</b><br><br>';
99       $tables .= sprintf('<div class="proxhr" id="table%d"></div>', $i);
100       $tables .= sprintf('<div class="table_act" id="table_act%d"></div>', $i);
101       $tables .= '</div></td>'."\n";
102       if ($i % 4 == 3)
103         $tables .= '</tr>';
104     }
105     $tables .= '<tr><td colspan="4">';
106     $tables .= '<div class="room_ex_standup">';
107     $tables .= '<b>Giocatori in piedi</b><br><br>';
108     
109     $tables .= sprintf('<div id="standup" class="room_standup"></div>');
110     $tables .= '<div id="esco" class="esco"></div>';
111     $tables .= '</td></tr>';
112     
113     $tables .= '</table>';
114   }
115
116 $brisk_header = '<div class="container">
117 <!-- =========== header ===========  -->
118 <div class="header">
119 <img class="nobo" src="img/brisk_logo64.png">
120 briscola chiamata in salsa ajax<br><br>
121 </div>
122
123 <!--  =========== vertical menu ===========  -->
124 <div class="topmenu">
125 <a target="_blank" href="/briskhome.php"><img class="nobo" src="img/brisk_homebutt.png"></a>
126 <br><br><br>
127 sponsored by:<br><br>
128 <a target="_blank" href="http://www.alternativeoutput.it"><img class="nobo" src="img/altout80x15.png"></a><br>
129 <a target="_blank" href="http://www.dynamica.it"><img class="nobo" src="img/dynamica.png"></a><br><br>
130 supported by:<br><br>
131 <a target="_blank" href="http://www.briscolachiamata.it"><img class="nobo" src="img/brichi.png"></a><br><br>
132 </div>';
133     
134   /* Templates. */
135   if ($ACTION == 'login') {
136 ?>
137 <html>
138 <head>
139 <title>Brisk</title>
140 <link rel="shortcut icon" href="img/brisk_ico.png">
141 <script type="text/javascript" src="dnd.js"></script>
142 <script type="text/javascript" src="dom-drag.js"></script>
143 <script type="text/javascript" src="commons.js"></script> 
144 <script type="text/javascript" src="xhr.js"></script>
145 <script type="text/javascript" src="preload_img.js"></script>
146 <link rel="stylesheet" type="text/css" href="room.css">
147 </head>
148 <body>
149 <SCRIPT type="text/javascript">
150    window.onload = function() {
151      $("nameid").focus();
152    }
153 </SCRIPT>
154 <?php
155     echo "$brisk_header";
156 ?> 
157
158 <!--  =========== tables ===========  -->
159 <div class="tables">
160 <?php echo "$body"; ?>
161
162 <br>
163 <div style="text-align: center;">
164    <br><br><br>
165 Digita il tuo nickname per accedere ai tavoli della briscola.<br><br>
166 <form method="post" action="">
167 <input id="nameid" name="name" type="text" size="24" maxlength="12" value="">
168 </form>
169 </div>
170 </div></div>
171 <br><br><br><br>
172
173 <div id="imgct"></div>
174 <div id="logz"></div>
175 <div id="sandbox"></div>
176 <div id="sandbox2"></div>
177 <div id="response"></div>
178 <div id="xhrstart"></div>
179 <pre>
180 <div id="xhrlog"></div>
181 </pre>
182 <div id="xhrdeltalog"></div>
183 </body>
184 </html>
185 <?php
186   }
187   else if ($ACTION == 'table') {
188   ?>
189 <html>
190 <head>
191 <title>Brisk</title>
192 <link rel="shortcut icon" href="img/brisk_ico.png">
193 <script type="text/javascript" src="dnd.js"></script>
194 <script type="text/javascript" src="dom-drag.js"></script>
195 <script type="text/javascript" src="commons.js"></script> 
196 <script type="text/javascript" src="xhr.js"></script>
197 <script type="text/javascript" src="preload_img.js"></script>
198 <link rel="stylesheet" type="text/css" href="room.css">
199 </head>
200 <body>
201 <SCRIPT type="text/javascript">
202    var sess;
203    var stat = "";
204    var subst = "";
205    var gst  = new globst();
206
207    var g_imgct= 0;
208    var g_imgtot = g_preload_img_arr.length;
209    var myfrom = "index_php";
210    window.onload = function() {
211 <?php
212 if ($BRISK_DEBUG == "debugtable") {
213 ?>
214      room_checkspace(12,8,50);
215 <?php
216 }
217 else {
218 ?>
219      // alert("INDEX START");
220      xhr_rd = createXMLHttpRequest();
221      sess = "<?php echo "$sess"; ?>";
222
223      window.onunload = onunload_cb;
224
225      setTimeout(xhr_rd_poll, 0, sess); 
226      // alert("ARR LENGTH "+g_preload_img_arr.length);
227      setTimeout(preload_images, 0, g_preload_img_arr, g_imgct); 
228      $("txt_in").focus();
229 <?php
230 }
231 ?>
232    }
233
234 </SCRIPT>
235 <?php
236     echo "$brisk_header";
237 ?> 
238 <!--  =========== tables ===========  -->
239 <div class="tables">
240 <input name="sess" type="hidden" value="<?php echo "$user->sess"; ?>">
241 <?php echo "$tables"; ?>
242 </div>
243
244 <!--  =========== bottom ===========  -->
245 <div class="bottom">
246 <b>Chat</b>
247 <div id="txt" class="chatt">
248 </div>
249 <table><tr><td><div id="myname" class="txtt"></div></td><td><input id="txt_in" type="text" size="90" maxlength="256" onkeypress="chatt_checksend(this,event);" class="txtt"></td></tr></table>
250 </div>
251 <div id="heartbit"></div>
252 <div id="sandbox"></div>
253 <div id="imgct"></div>
254 <div id="logz"></div>
255 <div id="sandbox2"></div>
256 <div id="response"></div>
257 <div id="remark"></div>
258 <div id="xhrstart"></div>
259 <div id="xhrlog"></div>
260 <div id="xhrdeltalog"></div>
261 </div>
262 </body>
263 </html>
264 <?php
265    }
266 }
267
268 main();
269
270 ?>