webstart layout enhancement
[brisk.git] / web / info_main.php
1 <?php
2 /*
3  *  brisk - info_main.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 define( FTOK_PATH, "/var/lib/brisk");
25 define(MAX_PLAYERS, 5);
26 define(SESS_LEN, 13);
27
28 if (MAX_PLAYERS == 5)
29      exit;
30
31 class User {
32   var $name;
33   var $sess;
34   var $table;
35   
36   function User($name, $sess, $table = "") {
37     $this->name =  $name;
38     $this->sess =  $sess;
39     $this->table = $table;
40   }
41 }
42
43 class brisco {
44   var $user;
45
46   function brisco () {
47     $this->user = array();
48     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
49       $this->user[$i] = new User("", "", "");
50     }
51   }
52 }
53
54 function init_data()
55 {
56   $brisco = new brisco();
57
58   return $brisco;
59 }
60
61 function load_data() 
62 {
63   if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) {
64     echo "FTOK FAILED";
65     exit;
66   }
67   echo "FTOK ".$tok."<br>";
68   if (($res = sem_get($tok)) == FALSE) {
69     echo "SEM_GET FAILED";
70     exit;
71   }
72   if (sem_acquire($res)) {
73     if ($shm = shm_attach($tok)) {
74       echo "fin qui<br>";
75       $bri = @shm_get_var($shm, $tok);
76     }
77     
78     shm_detach($shm);
79   }
80   sem_release($res);
81   
82   return ($bri);
83 }
84
85
86 function save_data($bri) 
87 {
88   $ret =   FALSE;
89   $shm =   FALSE;
90   $isacq = FALSE;
91
92   if (($tok = ftok(FTOK_PATH."/main", "B")) == -1) 
93     return (FALSE);
94
95   if (($res = sem_get($tok)) == FALSE) 
96     return (FALSE);
97
98   do {
99     if (sem_acquire($res) == FALSE) 
100       break;
101     $isacq = TRUE;
102
103     if (($shm = shm_attach($res)) == FALSE)
104       break;
105     
106     if (shm_put_var($shm, $res, $bri) == FALSE)
107       break;
108     $ret = TRUE;
109   } while (0);
110   
111   if ($shm)
112     shm_detach($shm);
113    
114   if ($isacq)
115     sem_release($res);
116   
117   return ($ret);
118 }
119
120 function &check_session($bri, $sess)
121 {
122   if (strlen($sess) == SESS_LEN) {
123     for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
124       if (strcmp($sess, $bri->user[$i]->sess) == 0) {
125         // find it
126         return ($bri->user[$i]);
127       }
128     }
129   }
130   for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
131     if ($bri->user[$i]->sess == "") {
132       $bri->user[$i]->sess = uniqid("");
133       return ($bri->user[$i]);
134     }
135   }
136
137   return (NULL);
138 }
139
140 function duplicated_name($bri, $name)
141 {
142   if (!isset($name))
143     return (FALSE);
144   
145   for ($i = 0 ; $i < MAX_PLAYERS ; $i++) {
146     if (strcmp($bri->user[$i]->name,$name) == 0) {
147       return (TRUE);
148     }
149   }
150   return (FALSE);
151 }
152
153 function main() {
154   GLOBAL $sess, $name;
155   $bri = load_data();
156
157   echo "<plaintext>";
158   var_dump($bri);
159 }
160
161 main();
162 ?>