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