Initial revision
[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  */
21
22 require_once("brisk.phh");
23 if (DEBUGGING == "local" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
24   echo "Debugging time!";
25   exit;
26 }
27
28 log_load($sess, "LOAD: index.php");
29
30 function main()
31 {
32   GLOBAL $sess, $name;
33   
34   $body = "";
35   $ACTION = "login";
36   
37   $is_table = false;
38   $sem = lock_data();
39   $bri = &load_data();
40   
41   /* Actions */
42   if (isset($sess)) {
43     $bri->garbage_manager(TRUE);
44     if (($user = &get_user(&$bri, $sess, &$idx)) != FALSE) {
45       if ($user->stat == "table") {
46         header ("Location: table.php");
47         unlock_data($sem);
48         exit;
49       }
50       $ACTION = "table";
51     }
52     else {
53       setcookie ("sess", "", time() - 3600);
54     }
55   }
56   else if (isset($name)) {
57     $bri->garbage_manager(TRUE);
58     /* try login */
59     if (($user = &add_user(&$bri, &$sess, &$idx, $name)) != FALSE) {
60       $ACTION = "table";
61
62       setcookie ("sess", "", time() + 180);      
63       standup_update(&$bri,&$user);
64
65       if (save_data(&$bri) == FALSE) {
66         echo "ERRORE SALVATAGGIO\n";
67         exit;
68       }
69     }
70     else {
71       /* Login Rendering */
72       if ($idx == -1) 
73         $body .= '<div class="urgmsg"><b>Spiacenti, non ci sono pi&ugrave; posti liberi. Riprova pi&ugrave; tardi.</b></div>';
74       else
75         $body .= '<div class="urgmsg"><b>Il tuo nickname &egrave; gi&agrave; in uso.</b></div>';
76     }
77   }
78   unlock_data($sem);
79
80   /* Rendering. */
81
82   if ($ACTION == "table") {
83     $tables .= '<table align="center" valign="center" border=1 cellpadding="12" cellspacing="0">';
84     for ($i = 0 ; $i < TABLES_N ; $i++) {
85       if ($i % 4 == 0)
86         $tables .= '<tr>';
87       $tables .= '<td valign="top" align="center" class="room_td"><b>Tavolo '.$i.'</b><br><br>';
88       $tables .= sprintf('<div class="proxhr" id="table%d"></div>', $i);
89       $tables .= sprintf('<div class="proxhr" id="table_act%d"></div>', $i);
90       $tables .= '</td>';
91       if ($i % 4 == 3)
92         $tables .= '</tr>';
93     }
94     $tables .= '<tr><td colspan="4">';
95     $tables .= '<div class="room_ex_standup">';
96     $tables .= '<b>Giocatori in piedi</b><br><br>';
97     
98     $tables .= sprintf('<div id="standup"></div>');
99     $tables .= '</div>';
100     $tables .= '</td></tr>';
101     
102     $tables .= '</table>';
103   }
104     
105   /* Templates. */
106   if ($ACTION == 'login') {
107 ?>
108 <html>
109 <head>
110 <title>Brisk</title>
111 <link rel="shortcut icon" href="img/brisk_ico.png">
112 <script type="text/javascript" src="dnd.js"></script>
113 <script type="text/javascript" src="dom-drag.js"></script>
114 <script type="text/javascript" src="commons.js"></script> 
115 <script type="text/javascript" src="xhr.js"></script>
116 <script type="text/javascript" src="preload_img.js"></script>
117 <link rel="stylesheet" type="text/css" href="brisk.css">
118 </head>
119 <body>
120 <SCRIPT type="text/javascript">
121    window.onload = function() {
122      $("nameid").focus();
123    }
124 </SCRIPT>
125 <img class="nobo" src="img/brisk_logo64.png">
126 <div style="text-align: center; font-size: 12px;">briscola chiamata in salsa ajax</div>
127 <br><br>
128 <?php echo "$body"; ?>
129
130 <br>
131 <div style="text-align: center;">
132 Digita il tuo nickname per accedere ai tavoli della briscola.<br><br>
133 <form method="post" action="">
134 <input id="nameid" name="name" type="text" maxlength="12" value="">
135 </form>
136 </div>
137 <br><br>
138 <hr>
139 <div id="imgct"></div>
140 <div id="logz"></div>
141 <div id="sandbox"></div>
142 <div id="sandbox2"></div>
143 <div id="response"></div>
144 <div id="xhrstart"></div>
145 <pre>
146 <div id="xhrlog"></div>
147 </pre>
148 <div id="xhrdeltalog"></div>
149 </body>
150 </html>
151 <?php
152   }
153   else if ($ACTION == 'table') {
154   ?>
155 <html>
156 <head>
157 <title>Brisk</title>
158 <link rel="shortcut icon" href="img/brisk_ico.png">
159 <script type="text/javascript" src="dnd.js"></script>
160 <script type="text/javascript" src="dom-drag.js"></script>
161 <script type="text/javascript" src="commons.js"></script> 
162 <script type="text/javascript" src="xhr.js"></script>
163 <script type="text/javascript" src="preload_img.js"></script>
164 <link rel="stylesheet" type="text/css" href="brisk.css">
165 </head>
166 <body>
167 <SCRIPT type="text/javascript">
168    var sess;
169    var stat = "";
170    var subst = "";
171    var gst  = new globst();
172
173    var g_imgct= 0;
174    var g_imgtot = g_preload_img_arr.length;
175    var myfrom = "index_php";
176    window.onload = function() {
177      // alert("INDEX START");
178      xhr_rd = createXMLHttpRequest();
179      sess = "<?php echo "$sess"; ?>";
180
181      setTimeout(xhr_rd_poll, 0, sess); 
182      // alert("ARR LENGTH "+g_preload_img_arr.length);
183      setTimeout(preload_images, 0, g_preload_img_arr, g_imgct); 
184    }
185 </SCRIPT>
186 <img class="nobo" src="img/brisk_logo64.png">
187 <div style="text-align: center; font-size: 12px;">briscola chiamata in salsa ajax</div><br>
188 <!-- <div><input name="logout" value="Esco." onclick="act_logout();" type="button"></div> -->
189 <input name="sess" type="hidden" value="<?php echo "$user->sess"; ?>">
190 <?php echo "$tables"; ?>
191
192 <b>Chat</b>
193 <div id="txt" class="chatt"></div>
194
195 <table><tr><td><div id="myname" class="txtt"></div></td><td><input id="txt_in" type="text" size="80" maxlength="256" onchange="act_chatt();" class="txtt"></td></tr></table>
196
197 <hr>
198 <div id="imgct"></div>
199 <div id="logz"></div>
200 <div id="sandbox"></div>
201 <div id="sandbox2"></div>
202 <div id="response"></div>
203 <div id="xhrstart"></div>
204 <pre>
205 <div id="xhrlog"></div>
206 </pre>
207 <div id="xhrdeltalog"></div>
208 </body>
209 </html>
210 <?php
211    }
212 }
213
214 main();
215
216 ?>