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